Home  >  Article  >  Java  >  How to parse Json data in Java

How to parse Json data in Java

黄舟
黄舟Original
2017-08-23 10:44:312001browse

The following editor will bring you an article about using Java to parse Json data (mutual nesting of object arrays). The editor thinks it’s pretty good, so I’ll share it with you now and give it as a reference. Let’s follow the editor and take a look.

During this period, we are making a small APP of English translation software, which involves the analysis of Json data, so I will summarize it here!

Suppose we want to parse the following data. In fact, in normal times, the returned Json data is very messy, and it is difficult to distinguish the relationship between the data. This is the result after being beautified by relevant tools


{
 "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"
 }
 ]
}

We define the data as a string. After adding escape characters, it becomes like this. It’s a bit messy, so let’s analyze it according to the picture above.

Json data is simply the nesting of objects and arrays, so let’s get started!


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


  JSONObject str = JSONObject.fromObject(st);  //json数据的最外层肯定是一个对象,参数为字符串
 //对象str又分为3部分:translation,basic,web 
  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));
 }  
  }
  if(str.has("web")){ //web是一个数组,每个数组元素又是三个Json对象
 System.out.println("拓展:");  
 JSONArray web = str.getJSONArray("web");  
 for(int i = 0; i<web.size() ; i++ ){ //但是对象中又嵌套着数组
  String t = web.getString(i); //遍历过程将web数组元素赋给String型变量
  JSONObject we = JSONObject.fromObject(t); //通过String又得到每个元素的对象
  if(we.has("key")){ 
 System.out.print(we.getString("key")+"\t");  //得到对象中的元素
  }
  if(we.has("value")){  
 JSONArray value = we.getJSONArray("value");
 for(int x = 0 ; x<value.size() ;x++ ){ //遍历对象中嵌套的数组
  System.out.print(value.getString(x));  //得到数组元素的值
  if(x<value.size()-1){
 System.out.print(";");
  }
 }
  }
  System.out.println();
 }
  }

The analysis is finished here!

The above is the detailed content of How to parse Json data in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn