一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|

服務器之家 - 編程語言 - JAVA教程 - 基于java解析JSON的三種方式詳解

基于java解析JSON的三種方式詳解

2020-07-20 13:05星辰之力 JAVA教程

這篇文章主要介紹了基于java解析JSON的三種方式,結合實例形式詳細分析了json解析的原理與GSON、FastJSON等常用解析操作技巧,需要的朋友可以參考下

本文實例分析了基于java解析JSON的三種方式。分享給大家供大家參考,具體如下:

一、什么是JSON?

JSON是一種取代XML的數據結構,和xml相比,它更小巧但描述能力卻不差,由于它的小巧所以網絡傳輸數據將減少更多流量從而加快速度。

JSON就是一串字符串 只不過元素會使用特定的符號標注。

{} 雙括號表示對象
[] 中括號表示數組
"" 雙引號內是屬性或值
: 冒號表示后者是前者的值(這個值可以是字符串、數字、也可以是另一個數組或對象)

所以 {"name": "Michael"} 可以理解為是一個包含name為Michael的對象

而[{"name": "Michael"},{"name": "Jerry"}]就表示包含兩個對象的數組

當然了,你也可以使用{"name":["Michael","Jerry"]}來簡化上面一部,這是一個擁有一個name數組的對象

二、JSON解析之傳統的JSON解析

1、生成json字符串

?
1
2
3
4
5
public static String createJsonString(String key, Object value) {
    JSONObject jsonObject = new JSONObject();
    jsonObject.put(key, value);
    return jsonObject.toString();
}

2、解析JSON字符串

分為以下三種情況,一個JavaBean,一個List數組,一個嵌套Map的List數組:

?
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
import com.android.myjson.domain.Person;
/**
 * 完成對json數據的解析
 *
 */
public class JsonTools {
  public static Person getPerson(String key, String jsonString) {
    Person person = new Person();
    try {
      JSONObject jsonObject = new JSONObject(jsonString);
      JSONObject personObject = jsonObject.getJSONObject("person");
      person.setId(personObject.getInt("id"));
      person.setName(personObject.getString("name"));
      person.setAddress(personObject.getString("address"));
    } catch (Exception e) {
      // TODO: handle exception
    }
    return person;
  }
  public static List getPersons(String key, String jsonString) {
    List list = new ArrayList();
    try {
      JSONObject jsonObject = new JSONObject(jsonString);
      // 返回json的數組
      JSONArray jsonArray = jsonObject.getJSONArray(key);
      for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject jsonObject2 = jsonArray.getJSONObject(i);
        Person person = new Person();
        person.setId(jsonObject2.getInt("id"));
        person.setName(jsonObject2.getString("name"));
        person.setAddress(jsonObject2.getString("address"));
        list.add(person);
      }
    } catch (Exception e) {
      // TODO: handle exception
    }
    return list;
  }
  public static List getList(String key, String jsonString) {
    List list = new ArrayList();
    try {
      JSONObject jsonObject = new JSONObject(jsonString);
      JSONArray jsonArray = jsonObject.getJSONArray(key);
      for (int i = 0; i < jsonArray.length(); i++) {
        String msg = jsonArray.getString(i);
        list.add(msg);
      }
    } catch (Exception e) {
      // TODO: handle exception
    }
    return list;
  }
  public static List> listKeyMaps(String key,
      String jsonString) {
    List> list = new ArrayList>();
    try {
      JSONObject jsonObject = new JSONObject(jsonString);
      JSONArray jsonArray = jsonObject.getJSONArray(key);
      for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject jsonObject2 = jsonArray.getJSONObject(i);
        Map map = new HashMap();
        Iterator iterator = jsonObject2.keys();
        while (iterator.hasNext()) {
          String json_key = iterator.next();
          Object json_value = jsonObject2.get(json_key);
          if (json_value == null) {
            json_value = "";
          }
          map.put(json_key, json_value);
        }
        list.add(map);
      }
    } catch (Exception e) {
      // TODO: handle exception
    }
    return list;
  }
}

三、JSON解析之GSON

1、生成JSON字符串

?
1
2
3
4
5
6
7
8
import com.google.gson.Gson;
public class JsonUtils {
  public static String createJsonObject(Object obj) {
    Gson gson = new Gson();
    String str = gson.toJson(obj);
    return str;
  }
}

2、解析JSON

?
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
;
public class GsonTools {
  public GsonTools() {
    // TODO Auto-generated constructor stub
  }
  /**
   * @param
   * @param jsonString
   * @param cls
   * @return
   */
  public static T getPerson(String jsonString, Class cls) {
    T t = null;
    try {
      Gson gson = new Gson();
      t = gson.fromJson(jsonString, cls);
    } catch (Exception e) {
      // TODO: handle exception
    }
    return t;
  }
  /**
   * 使用Gson進行解析 List
   *
   * @param
   * @param jsonString
   * @param cls
   * @return
   */
  public static List getPersons(String jsonString, Class cls) {
    List list = new ArrayList();
    try {
      Gson gson = new Gson();
      list = gson.fromJson(jsonString, new TypeToken>() {
      }.getType());
    } catch (Exception e) {
    }
    return list;
  }
  /**
   * @param jsonString
   * @return
   */
  public static List getList(String jsonString) {
    List list = new ArrayList();
    try {
      Gson gson = new Gson();
      list = gson.fromJson(jsonString, new TypeToken>() {
      }.getType());
    } catch (Exception e) {
      // TODO: handle exception
    }
    return list;
  }
  public static List> listKeyMaps(String jsonString) {
    List> list = new ArrayList>();
    try {
      Gson gson = new Gson();
      list = gson.fromJson(jsonString,
          new TypeToken>>() {
          }.getType());
    } catch (Exception e) {
      // TODO: handle exception
    }
    return list;
  }
}

四、JSON解析之FastJSON

?
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
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
public class JsonTool {
  public static T getPerson(String jsonstring, Class cls) {
    T t = null;
    try {
      t = JSON.parseObject(jsonstring, cls);
    } catch (Exception e) {
      // TODO: handle exception
    }
    return t;
  }
  public static List getPersonList(String jsonstring, Class cls) {
    List list = new ArrayList();
    try {
      list = JSON.parseArray(jsonstring, cls);
    } catch (Exception e) {
      // TODO: handle exception
    }
    return list;
  }
  public static List> getPersonListMap1(
      String jsonstring) {
    List> list = new ArrayList>();
    try {
      list = JSON.parseObject(jsonstring,
          new TypeReference>>() {
          }.getType());
    } catch (Exception e) {
      // TODO: handle exception
    }
    return list;
  }
}

總結:

JSON對于移動設備來說,尤其對于網絡環境較差和流量限制的情況下,相對于XML格式的數據傳輸會更節省流量,傳輸效率更高。在這三種解析方式中FastJson是效率最高的,推薦使用。

希望本文所述對大家java程序設計有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 成年美女黄网站色视频大全免费 | juy799大岛优香在线观看 | 日韩亚洲人成在线 | 亚洲欧美一区二区久久 | 俄罗斯年轻男同gay69 | b站免费| 亚洲经典 | 美国video | 久久99国产视频 | 精品国产中文字幕在线视频 | 精品国产免费第一区二区三区日韩 | 99re热这里只有精品 | 嗯啊好大视频 | 国产在线99 | 成年人免费在线看的惊悚动作片 | fc2成人免费共享视频 | 亚洲国产精品无码中文在线 | 激情图片 激情小说 | 美女班主任让我爽了一夜视频 | 娇喘高潮教室h | 风间由美一区二区av101 | 亚洲天堂成人在线 | 成人在线免费观看 | 青青草伊人久久 | 亚洲欧美综合一区 | 天堂精品高清1区2区3区 | chinese特色video | 99在线视频观看 | 纲手被漫画aⅴ | 日韩精品欧美国产精品亚 | 亚洲欧美成人综合在线 | 奇米影视亚洲狠狠色 | 国内自拍2019 | 天堂在线免费观看 | 亚洲视频在线观看免费视频 | 国产无限免费观看黄网站 | 久久香蕉国产免费天天 | 草莓视频幸福宝 | 成人国产精品一区二区不卡 | 欧美极品摘花过程 | 超级毛片 |