1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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( "异常" );
            }
 
      }
}
GitHub 加速计划 / js / json
41.72 K
6.61 K
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:1 个月前 )
960b763e 3 个月前
8c391e04 6 个月前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐