星空网 > 软件开发 > Java

json字符串转换为JSONObject和JSONArray

import org.json.JSONArray;import org.json.JSONObject;public class JSONTest{  public static void main(String[] args){   //JSONObject   String jsonMessage = "{\"语文\":\"88\",\"数学\":\"78\",\"计算机\":\"99\"}";   String value1 = null;   try{      //将字符串转换成jsonObject对象     JSONObject myJsonObject = new JSONObject(jsonMessage);     //获取对应的值     value1 = myJsonObject.getString("数学");   } catch (JSONException e){     System.out.println("异常");   }   System.out.println("value1="+value1);    //JSONArray  jsonMessage = "[{'num':'成绩', '外语':88, '历史':65, '地理':99, 'object':{'aaa':'1111','bbb':'2222','cccc':'3333'}}," +      "{'num':'兴趣', '外语':28, '历史':45, '地理':19, 'object':{'aaa':'11a11','bbb':'2222','cccc':'3333'}}," +      "{'num':'爱好', '外语':48, '历史':62, '地理':39, 'object':{'aaa':'11c11','bbb':'2222','cccc':'3333'}}]";   JSONArray myJsonArray;    try{       myJsonArray = new JSONArray(jsonMessage);       for(int i=0 ; i < myJsonArray.length() ;i++) {        //获取每一个JsonObject对象        JSONObject myjObject = myJsonArray.getJSONObject(i);          //获取每一个对象中的值         String numString = myjObject.getString("num");         int englishScore = myjObject.getInt("外语");         int historyScore = myjObject.getInt("历史");         int geographyScore = myjObject.getInt("地理");         //获取数组中对象的对象         JSONObject myjObject2 = myjObject.getJSONObject("object");         String aaaString = myjObject2.getString("aaa");         System.out.println("aaaString="+aaaString);         System.out.println("numString="+numString);         System.out.println("englishScore="+englishScore);         System.out.println("historyScore="+historyScore);         System.out.println("geographyScore="+geographyScore);       }      }catch (JSONException e){          System.out.println("异常");      }   }}

  




原标题:json字符串转换为JSONObject和JSONArray

关键词:JS

JS
*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

买单报关:https://www.ikjzd.com/w/1573202449918582786
海天下:https://www.ikjzd.com/w/1573202450518024193
卖家港湾:https://www.ikjzd.com/w/1573202454343573506
麦家在线:https://www.ikjzd.com/w/1573202464342794242
邮件营销:https://www.ikjzd.com/w/1573202467089719297
mailigen:https://www.ikjzd.com/w/1573202474895319041
在古巴做游轮 古巴旅游项目:https://www.vstour.cn/a/363194.html
西藏旅游攻略自驾游需要多少天 去西藏旅游自驾游要多久时间:https://www.vstour.cn/a/363195.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流