什么是 Json?
  JSON(JvaScript Object Notation)(官网网站:http://www.json.org/)是 一种轻量级的数据交换格式。 
  易于人阅读和编写。同时也易于机器解析和生成。它基于 JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999 的一个子集。
  JSON 采用完全独立于语言的文本格式,但是也使用了类似于 C 语言家族的习惯(包括C, C++, C#, Java, JavaScript, Perl, Python等)。 这些特性使 JSON 成为理想的数据交换语言。

JSON 的两种结构
 1.“名称/值”对的集合(A collection of name/value pairs)。
  不同的语言中,它被理解为对象(object),纪录(record),结构(struct),字典(dictionary),哈希表 (hash table),有键列表(keyed list),或者关联数组 (associative array)。
  在 Java 语言中,我们可以将它理解成 HashMap。
  对象是一个无序的"'名称/值'对"集合。一个对象以"{"(左括号)开始,"}"(右括号)结束。每个“名称”后跟一个":"(冒号);"'名称/值' 对"之间使用","(逗号)分隔。
  示例:var json = {"name":"Jack","age":90,"Marray":true};

  Java中json的构造和解析

 2. 值的有序列表(An ordered list of values)。
  在大部分语言中,它被理解为数组(Array 或 List)。
  数组是值(value)的有序集合。一个数组以"["(左中括号)开始,"]"(右中括号)结束。值之间使用","(逗号)分隔。
  示例:var json = ["Jack","Rose","Tom",89,true,false];

   Java中json的构造和解析

 

Java中对json的操作常用的方式有两种:org.json.jar 和 json-lib.jar

一、使用org.json.jar(下载)

   

 1 import java.util.ArrayList;
 2 import java.util.List;
 3 import org.json.JSONArray;
 4 import org.json.JSONObject;
 5 import com.kevin.paper.entity.PubPaper;
 6 import com.kevin.paper.entity.PubPaperAuthor;
 7 
 8 public class Test {
 9     public String jsonConstruct(){  
10         JSONObject json=new JSONObject();
11         json.put("Article_id", "66496");
12         json.put("Title", "Exploring the beta quadrant");
13         json.put("Author", "Pietarinen, Ahti-Veikko");
14         json.put("Journal", "SYNTHESE");
15         json.put("ISSN", "0039-7857");
16         json.put("PublishDate", "2015-04-01");
17         json.put("Pages", "941-970");
18         json.put("SCI", "true");
19         json.put("EI", "false");
20         json.put("SSCI", "true");
21         //嵌套json
22         JSONArray jsonMembers = new JSONArray();
23         JSONObject author1 = new JSONObject();  
24         author1.put("ZZGH", "150026");  
25         author1.put("ZZXM", "AHTI-VEIKKO PIETARINEN");  
26         author1.put("SFDYZZ","1");  
27         author1.put("SFTXZZ", "1");  
28         jsonMembers.put(author1);  
29         json.put("Authors", jsonMembers);  
30       
31         return json.toString();  
32     }
33     
34     public PubPaper jsonAnalysis(){
35         PubPaper pubPaper = new PubPaper();
36         String jsonData="{\"Article_id\":66496,\"Title\":\"Exploring the beta quadrant\",\"Author\":\"Pietarinen, Ahti-Veikko\"," +
37                 "\"Journal\":\"SYNTHESE\",\"ISSN\":\"0039-7857\",\"PublishDate\":\"2015-04-01\",\"Pages\":\"941-970\"," +
38                 "\"SCI\":\"true\",\"EI\":\"false\",\"SSCI\":\"true\"," +
39                 "\"Authors\":[{\"ZZGH\":\"150026\",\"ZZXM\":\"AHTI-VEIKKO PIETARINEN\",\"SFDYZZ\":1,\"SFTXZZ\":1}]}";  
40         JSONObject json= new JSONObject(jsonData);
41         pubPaper.setArticle_id(json.get("Article_id").toString());
42         pubPaper.setTitle(json.get("Title").toString());
43         pubPaper.setAuthor(json.get("Author").toString());
44         pubPaper.setJournal(json.get("Journal").toString());
45         pubPaper.setISSN(json.get("ISSN").toString());
46         pubPaper.setPublishDate(json.get("PublishDate").toString());
47         pubPaper.setPages(json.get("Pages").toString());
48         pubPaper.setSCI(json.get("SCI").toString());
49         pubPaper.setEI(json.get("EI").toString());
50         pubPaper.setSSCI(json.get("SSCI").toString());
51         //解析嵌套的json转换成对象集合
52         List<PubPaperAuthor> pubAuthorList = new ArrayList();
53         JSONArray jsonArray=json.getJSONArray("Authors");  
54         for(int i=0;i<jsonArray.length();i++){  
55             JSONObject author=(JSONObject) jsonArray.getJSONObject(i);
56             PubPaperAuthor pubPaperAuthor = new PubPaperAuthor();
57             pubPaperAuthor.setZZGH(author.get("ZZGH").toString());
58             pubPaperAuthor.setZZXM(author.get("ZZXM").toString());
59             pubPaperAuthor.setSFDYZZ(author.get("SFDYZZ").toString());
60             pubPaperAuthor.setSFTXZZ(author.get("SFTXZZ").toString());
61             pubAuthorList.add(pubPaperAuthor);
62         }
63         pubPaper.setAuthors(pubAuthorList);
64         return pubPaper;  
65     }  
66 }
View Code

相关文章:

  • 2021-10-07
  • 2021-09-20
  • 2022-12-23
猜你喜欢
  • 2021-12-25
  • 2021-05-22
  • 2022-12-23
  • 2022-01-16
  • 2022-12-23
  • 2022-02-10
  • 2022-01-14
相关资源
相似解决方案