Android JSON解析json数据

一、什么是JSON?

JSON是一种取代XML的数据结构,和xml相比,它更小巧但描述能力却不差,由于它的小巧所以网络传输数据将减少更多流量从而加快速度。
JSON就是一串字符串 只不过元素会使用特定的符号标注。
{} 双括号表示对象
[] 中括号表示数组
“” 双引号内是属性或值
: 冒号表示后者是前者的值(这个值可以是字符串、数字、也可以是另一个数组或对象)
案例1:

{
  "translation": [
     "爱"
   ],
  "basic": {
    "us-phonetic": "lʌv",
     "phonetic": "lʌv",
    "uk-phonetic": "lʌv",
    "explains": [
      "n. 恋爱;亲爱的;酷爱;喜爱的事物",
      "vt. 喜欢;热爱;爱慕",
      "vi. 爱",
      "n. (Love)人名;(英)洛夫"
    ]
   },
   "web": [
     {
     "value": [
        "爱情",
         "爱",
        "爱"
      ],
      "key": "Love"
     },
     {
     "value": [
        "无尽的爱",
       "蓝色生死恋",
        "不了情"
      ],
     "key": "Endless Love"
    },
    {
      "value": [
        "早恋",
         "青春期恋爱",
        "初恋"
       ],
       "key": "puppy love"
     }
   ]
}

JSONObject str = JSONObject.fromObject(st); 
if(str.has("translation")){         
          JSONArray tr = str.getJSONArray("translation"); //translation是一个json数组
           for(int i = 0 ;i<tr.size();i++){                //对数组元素进行遍历
              System.out.println(tr.getString(i));       //因为元素的值是String类型的所以忽的值的方法是getString(index)
            }
       } 
                                                                                                                                                               //basic中有数组也有元素
         if(str.has("basic")){                             
            JSONObject us = str.getJSONObject("basic"); //对元素进行解析,并输出元素的值
           System.out.print("美:[" + us.getString("us-phonetic") + "]\t");     
             JSONObject uk = str.getJSONObject("basic");
            System.out.print("英:[" + us.getString("uk-phonetic") + "]\n");
            JSONObject basic = str.getJSONObject("basic"); //先获得basic对象
            JSONArray explain = basic.getJSONArray("explains"); //再获得basic对象下的explains数组 
           for(int i = 0;i<explain.size(); i++){   //对数组元素进行遍历
                System.out.println(explain.getString(i));
            }     
         }

案例2:

[
    {
        "ver": "20200624",
        "model": "M86",
        "size": "81977",
        "md5": "",
        "url": "http://static.ococm.com/8386/sugarcane-ota-TVOS-02.04.03.20200624.zip"
    },
    {
        "ver": "20200624",
        "model": "M76",
        "size": "81977",
        "md5": "",
        "url": "http://static.ococm.com/8386/sugarcane-ota20200624.zip"
    },
    {
        "ver": "20200624",
        "model": "M96",
        "size": "81977",
        "md5": "",
        "url": "http://static.ococm.com/8386/sugarcane-ota-20200624.zip"
    }
]

JSONArray jsonArray = new JSONArray(str);
            for(int i=0;i<jsonArray.length();i++){
                JSONObject jsonObject = (JSONObject)jsonArray.get(i);
                String model = jsonObject.getString("model");
                 if(model.equals("M86")){
                     mVersionInfo.setVersion(jsonObject.getString("ver"));
                     mVersionInfo.setUrl(jsonObject.getString("url"));
                    mVersionInfo.setSize(jsonObject.getLong("size"));
                    mVersionInfo.setMd5(jsonObject.getString("md5"));
                     break;
			    }
			}

你可能感兴趣的:(android)