关于json的知识点

来源:互联网 发布:淘宝登录框打不开 编辑:程序博客网 时间:2024/05/17 01:54

//将字符串转换成jsonObject对象

JSONObject myJsonObject = new JSONObject(jsonMessage);

 a.JSONObject

        这是系统中有关JSON定义的基本单元,其包含一对儿(Key/Value)数值。

{     "Image": {      "Width":  800,      "Thumbnail": {          "Url":    "http://www.example.com/image/481989943",      },      "IDs": [116, 943, 234, 38793]    }}

 b.JSONArray

       它代表一组有序的数值。将其转换为String输出(toString)所表现的形式是用方括号包裹,数值以逗号”,”分隔(例如:[value1,value2,value3]

[   {      "precision": "zip",      "Latitude":  37.7668,      "Longitude": -122.3959,   },   {      "precision": "zip",      "Latitude":  37.371991,      "Longitude": -122.026020,   }]


通过json自动遍历解析里面的key和value的值,并且去空。

public static RequestBody JsonStrTrim(String jsonStr) {    JSONObject reagobj = null;    RequestBody body;    FormEncodingBuilder formEncodingBuilder = new FormEncodingBuilder();    try {        reagobj = new JSONObject(jsonStr);    } catch (JSONException e) {        e.printStackTrace();    }    // 取出 jsonObject 中的字段的值的空格    Iterator itt = reagobj.keys();    while (itt.hasNext()) {        String key = itt.next().toString();        String value = null;        try {            value = reagobj.getString(key);            if (value == null) {                continue;            } else if ("".equals(value.trim())) {                continue;            } else {                reagobj.put(key, value.trim());            }        } catch (JSONException e) {            e.printStackTrace();        }        formEncodingBuilder.add(key, value);    }    body = formEncodingBuilder.build();    return body;}
      对url的截取代码操作
  1.  public static String getValueByName(String url, String name) {  
  2.         String result = "";  
  3.         int index = url.indexOf("?");  
  4.         String temp = url.substring(index + 1);  
  5.         String[] keyValue = temp.split("\\&");  
  6.         for (String str : keyValue) {  
  7.             if (str.contains(name)) {  
  8.                 result = str.replace(name + "=""");  
  9.             }  
  10.         }  
  11.         return result;  
  12.     }  

0 0