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

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

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - Simple JSON開發指南

Simple JSON開發指南

2020-04-22 11:40蝦米 JAVA教程

注意:JSONPauser不是線程安全的,需要的朋友可以參考下

Simple JSON是Google開發的Java JSON解析框架,基于Apache協議。

下載的文件是:json_simple.jar

例子1:很方便的方式,使用JSONValue

?
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
System.out.println("=======decode=======");
   
   String s="[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]";
   Object obj=JSONValue.parse(s);
   JSONArray array=(JSONArray)obj;
   System.out.println("======the 2nd element of array======");
   System.out.println(array.get(1));
   System.out.println();
       
   JSONObject obj2=(JSONObject)array.get(1);
   System.out.println("======field \"1\"==========");
   System.out.println(obj2.get("1")); 
 
       
   s="{}";
   obj=JSONValue.parse(s);
   System.out.println(obj);
       
   s="[5,]";
   obj=JSONValue.parse(s);
   System.out.println(obj);
       
   s="[5,,2]";
   obj=JSONValue.parse(s);
   System.out.println(obj);

JSONObject是繼承Map的,而JSONArray是繼承List的,所以你可以用Map和List的標準方式來使用JSONObject和JSONArray。

而JSONValue則可以使用數組也可以用對象。

例子2:快速的方式,使用JSONParser

?
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
JSONParser parser=new JSONParser();
 
 System.out.println("=======decode=======");
     
 String s="[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]";
 Object obj=parser.parse(s);
 JSONArray array=(JSONArray)obj;
 System.out.println("======the 2nd element of array======");
 System.out.println(array.get(1));
 System.out.println();
     
 JSONObject obj2=(JSONObject)array.get(1);
 System.out.println("======field \"1\"==========");
 System.out.println(obj2.get("1")); 
 
     
 s="{}";
 obj=parser.parse(s);
 System.out.println(obj);
     
 s="[5,]";
 obj=parser.parse(s);
 System.out.println(obj);
     
 s="[5,,2]";
 obj=parser.parse(s);
 System.out.println(obj);

 使用JSONParser需要捕獲異常。

例子3:異常處理

?
1
2
3
4
5
6
7
8
9
10
String jsonText = "[[null, 123.45, \"a\\tb c\"]}, true";
 JSONParser parser = new JSONParser();
     
 try{
 parser.parse(jsonText);
 }
 catch(ParseException pe){
 System.out.println("position: " + pe.getPosition());
 System.out.println(pe);
 }

執行結果:

?
1
position:25 Unexpected token RIGHT BRACE(}) at position 25.

 

例子4:容器工廠

使用使用ContainerFactory類來創建一個容器工廠。

?
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
String jsonText = "{\"first\": 123, \"second\": [4, 5, 6], \"third\": 789}";
 JSONParser parser = new JSONParser();
 ContainerFactory containerFactory = new ContainerFactory(){
 public List creatArrayContainer() {
  return new LinkedList();
 }
 
 public Map createObjectContainer() {
  return new LinkedHashMap();
 }
       
 };
     
 try{
 Map json = (Map)parser.parse(jsonText, containerFactory);
 Iterator iter = json.entrySet().iterator();
 System.out.println("==iterate result==");
 while(iter.hasNext()){
  Map.Entry entry = (Map.Entry)iter.next();
  System.out.println(entry.getKey() + "=>" + entry.getValue());
 }
       
 System.out.println("==toJSONString()==");
 System.out.println(JSONValue.toJSONString(json));
 }
 catch(ParseException pe){
 System.out.println(pe);
 }

 結果如下:

?
1
2
==iterate result== first=>123 second=>[4,5,6] third=>789 ==toJSONString()==
 {"first":123,"second":[4,5,6],"third":789}
 如果你不使用容器工廠,Simple-JSON默認使用JSONObject和JSONArray。
 例子5:可停的SAX式內容處理
SimpleJSON推薦一種簡單的可停的SAX方式的內容處理方式來處理文本流,用戶可以停留在邏輯輸入流的任意點,接著去處理其他邏輯,然后再繼續先前的處理。不用等待整個流處理完畢。以下是一個例子。
KeyFinder.java:
?
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
class KeyFinder implements ContentHandler{
 private Object value;
 private boolean found = false;
 private boolean end = false;
 private String key;
 private String matchKey;
   
 public void setMatchKey(String matchKey){
 this.matchKey = matchKey;
 }
   
 public Object getValue(){
 return value;
 }
   
 public boolean isEnd(){
 return end;
 }
   
 public void setFound(boolean found){
 this.found = found;
 }
   
 public boolean isFound(){
 return found;
 }
   
 public void startJSON() throws ParseException, IOException {
 found = false;
 end = false;
 }
 
 public void endJSON() throws ParseException, IOException {
 end = true;
 }
 
 public boolean primitive(Object value) throws ParseException, IOException {
 if(key != null){
  if(key.equals(matchKey)){
  found = true;
  this.value = value;
  key = null;
  return false;
  }
 }
 return true;
 }
 
 public boolean startArray() throws ParseException, IOException {
 return true;
 }
 
   
 public boolean startObject() throws ParseException, IOException {
 return true;
 }
 
 public boolean startObjectEntry(String key) throws ParseException, IOException {
 this.key = key;
 return true;
 }
   
 public boolean endArray() throws ParseException, IOException {
 return false;
 }
 
 public boolean endObject() throws ParseException, IOException {
 return true;
 }
 
 public boolean endObjectEntry() throws ParseException, IOException {
 return true;
 }
}
Main logic:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
String jsonText ="{\"first\": 123, \"second\": [{\"k1\":{\"id\":\"id1\"}}, 4, 5, 6, {\"id\": 123}], \"third\": 789, \"id\": null}";
 JSONParser parser =newJSONParser();
 KeyFinder finder =newKeyFinder();
 finder.setMatchKey("id");
 try{
 while(!finder.isEnd()){
  parser.parse(jsonText, finder,true);
  if(finder.isFound()){
  finder.setFound(false);
  System.out.println("found id:");
  System.out.println(finder.getValue());
  }
 }  
 }
 catch(ParseException pe){
 pe.printStackTrace();
 }
執行結果:
?
1
2
3
4
5
6
found id:
 id1
 found id:
 123
 found id:
 null
例子6:整個對象圖,用SAX式的解析
?
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
class Transformer implements ContentHandler{
  private Stack valueStack;
   
  public Object getResult(){
   if(valueStack == null || valueStack.size() == 0)
    return null;
   return valueStack.peek();
  }
   
  public boolean endArray () throws ParseException, IOException {
   trackBack();
   return true;
  }
 
  public void endJSON () throws ParseException, IOException {}
 
  public boolean endObject () throws ParseException, IOException {
   trackBack();
   return true;
  }
 
  public boolean endObjectEntry () throws ParseException, IOException {
   Object value = valueStack.pop();
   Object key = valueStack.pop();
   Map parent = (Map)valueStack.peek();
   parent.put(key, value);
   return true;
  }
 
  private void trackBack(){
   if(valueStack.size() > 1){
    Object value = valueStack.pop();
    Object prev = valueStack.peek();
    if(prev instanceof String){
     valueStack.push(value);
    }
   }
  }
   
  private void consumeValue(Object value){
   if(valueStack.size() == 0)
    valueStack.push(value);
   else{
    Object prev = valueStack.peek();
    if(prev instanceof List){
     List array = (List)prev;
     array.add(value);
    }
    else{
     valueStack.push(value);
    }
   }
  }
   
  public boolean primitive (Object value) throws ParseException, IOException {
   consumeValue(value);
   return true;
  }
 
  public boolean startArray () throws ParseException, IOException {
   List array = new JSONArray();
   consumeValue(array);
   valueStack.push(array);
   return true;
  }
 
  public void startJSON () throws ParseException, IOException {
   valueStack = new Stack();
  }
 
  public boolean startObject () throws ParseException, IOException {
   Map object = new JSONObject();
   consumeValue(object);
   valueStack.push(object);
   return true;
  }
 
  public boolean startObjectEntry (String key) throws ParseException, IOException {
   valueStack.push(key);
   return true;
  }
   
 }

Main方式邏輯:

?
1
2
3
4
5
6
7
String jsonString = <Input JSON text>;
 Object value = null;
 JSONParser parser = new JSONParser();
 Transformer transformer = new Transformer();
   
 parser.parse(jsonString, transformer);
 value = transformer.getResult();
 執行結果:
?
1
2
3
4
String jsonString =<Input JSON text>;
 Object value =null;
 JSONParser parser =newJSONParser();
 value = parser.parse(jsonString);
注意:
JSONPauser不是線程安全的。 

json_encode — 對變量進行 JSON 編碼。

說明:string json_encode ($value ),返回 value 值的 JSON 形式。

參數:待編碼的 value ,除了resource 類型之外,可以為任何數據類型

該函數只能接受 UTF-8 編碼的數據(譯注:指字符/字符串類型的數據)

返回值:編碼成功則返回一個以 JSON 形式表示的 string 。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 紧身短裙女教师波多野 | 黑人巨大精品战中国美女 | 农村脱精光一级 | 2019nv天堂| 99re热| tk白丝丨vk| 免费成年人在线视频 | 国内精品露脸在线视频播放 | 幻女free性zoz0交 | 免费在线视频观看 | 青青青国产在线观看 | 亚洲天堂成人在线 | 久久全国免费久久青青小草 | 欧美成年黄网站色高清视频 | 久九九精品免费视频 | 国模孕妇季玥全部人体写真 | 激情视频网址 | 欧亚精品一区二区三区 | 精品福利一区二区免费视频 | 好大好硬好深好爽想要之黄蓉 | 欧美美女一级片 | 国产亚洲精品日韩香蕉网 | porno日本大学生高清 | 出a级黑粗大硬长爽猛视频 吃胸膜奶视频456 | 手机在线免费观看日本推理片 | 国产在线98福利播放视频免费 | 免费在线观看网址入口 | 久久中文字幕无线观看 | 亚洲大片免费观看 | 午夜久久久久久亚洲国产精品 | 女教师巨大乳孔中文字幕免费 | 久久亚洲国产成人影院 | 国产丰满美女做爰 | 成人在线视频观看 | 成年人免费在线视频 | 欧美人体高清在线观看ggogo | 办公室强行丝袜秘书啪啪 | 国产剧情麻豆刘玥视频 | 免费看美女被靠到爽 | www.日日爱 | 猫咪色网|