JSON 学习笔记

JSON 学习笔记

原创编写: 王宇 
2016-11-09


 

 


JSON 是如何存储信息

  1. {
  2. "book":[
  3. {
  4. "id":"01",
  5. "language":"Java",
  6. "edition":"third",
  7. "author":"Herbert Schildt"
  8. },
  9. {
  10. "id":"07",
  11. "language":"C++",
  12. "edition":"second",
  13. "author":"E.Balagurusamy"
  14. }
  15. ]
  16. }

基本语法

  • 语法
    • Data is represented in name/value pairs. (key value模式)
    • Curly braces hold objects and each name is followed by ‘:’(colon), the name/value pairs are separated by , (comma).(key value 分号分开)
    • Square brackets hold arrays and values are separated by ,(comma). (逗号分开每个Key Value)
  • 数据结构
    • {} 集合(Collection) or 对象(Object)
    • [] 数组 (Array)

JSON 数据类型

TypeDescription
Numberdouble- precision floating-point format in JavaScript
Stringdouble-quoted Unicode with backslash escaping
Booleantrue or false
Arrayan ordered sequence of values
Valueit can be a string, a number, true or false, null etc
Objectan unordered collection of key:value pairs
Whitespacecan be used between any pair of tokens
nullempty

JSON Schema

  • Schema 
    当我们在描述 文字链接 的时候,需要约定数据的组织方式,比如,需要知道有哪些字段,这些字段的取值如何表示等,这就是 JSON Schema 的来源。
  • 用途

    1. 用于描述数据结构
    2. 用于构建人机可读的文档
    3. 用于生成模拟数据
    4. 用于校验数据,实现自动化测试
  • 关键字

KeywordsDescription
$schemaThe $schema keyword states that this schema is written according to the draft v4 specification.
titleYou will use this to give a title to your schema.
descriptionA little description of the schema.
typeThe type keyword defines the first constraint on our JSON data: it has to be a JSON Object.
propertiesDefines various keys and their value types, minimum and maximum values to be used in JSON file.
requiredThis keeps a list of required properties.
minimumThis is the constraint to be put on the value and represents minimum acceptable value.
exclusiveMinimumIf “exclusiveMinimum” is present and has boolean value true, the instance is valid if it is strictly greater than the value of “minimum”.
maximumThis is the constraint to be put on the value and represents maximum acceptable value.
exclusiveMaximumIf “exclusiveMaximum” is present and has boolean value true, the instance is valid if it is strictly lower than the value of “maximum”.
multipleOfA numeric instance is valid against “multipleOf” if the result of the division of the instance by this keyword’s value is an integer.
maxLengthThe length of a string instance is defined as the maximum number of its characters.
minLengthThe length of a string instance is defined as the minimum number of its characters.
patternA string instance is considered valid if the regular expression matches the instance successfully.

JSON 同 XML 比较

  • 冗长(Verbose) 
    XML 比JSON 有过多的冗长因素,JSON对于程序员更容易编写
  • 数组的使用 
    XML 结构化数据,没有JSON 的数组
  • Parsing 
    JavaScript’s eval method parses JSON. When applied to JSON, eval returns the described object.

环境

下载配置 json-simple.jar , 在CLASSPATH中指向此包

JSON 同 Java 实体的对比

JSONJAVA
stringjava.lang.String
numberjava.lang.Number
true,falsejava.lang.Boolean
nullnull
arrayjava.util.List
objectjava.util.Map

Java 中形成JSON

  • 例子
  1. import org.json.simple.JSONObject;
  2. classJsonEncodeDemo{
  3. publicstaticvoid main(String[] args){
  4. JSONObject obj =newJSONObject();
  5. obj.put("name","foo");
  6. obj.put("num",newInteger(100));
  7. obj.put("balance",newDouble(1000.21));
  8. obj.put("is_vip",newBoolean(true));
  9. System.out.print(obj);
  10. }
  11. }
  • 输出结果
  1. {"balance":1000.21,"num":100,"is_vip":true,"name":"foo"}

Java 中解析JSON

  • 例子
  1. import org.json.simple.JSONObject;
  2. import org.json.simple.JSONArray;
  3. import org.json.simple.parser.ParseException;
  4. import org.json.simple.parser.JSONParser;
  5. classJsonDecodeDemo{
  6. publicstaticvoid main(String[] args){
  7. JSONParser parser =newJSONParser();
  8. String s ="[0,{\"1\":{\"2\":{\"3\":{\"4\":[5,{\"6\":7}]}}}}]";
  9. try{
  10. Object obj = parser.parse(s);
  11. JSONArray array =(JSONArray)obj;
  12. System.out.println("The 2nd element of array");
  13. System.out.println(array.get(1));
  14. System.out.println();
  15. JSONObject obj2 =(JSONObject)array.get(1);
  16. System.out.println("Field \"1\"");
  17. System.out.println(obj2.get("1"));
  18. s ="{}";
  19. obj = parser.parse(s);
  20. System.out.println(obj);
  21. s ="[5,]";
  22. obj = parser.parse(s);
  23. System.out.println(obj);
  24. s ="[5,,2]";
  25. obj = parser.parse(s);
  26. System.out.println(obj);
  27. }catch(ParseException pe){
  28. System.out.println("position: "+ pe.getPosition());
  29. System.out.println(pe);
  30. }
  31. }
  32. }
  • 输出结果
  1. The2nd element of array
  2. {"1":{"2":{"3":{"4":[5,{"6":7}]}}}}
  3. Field"1"
  4. {"2":{"3":{"4":[5,{"6":7}]}}}
  5. {}
  6. [5]
  7. [5,2]

参考

 

Useful Links on JSON 
Site on JSON - JSON’s Official site giving link on JSON specification, news, update etc. 
http://www.json.org/ 
The JavaTM Tutorials -The Java Tutorials are practical guides for programmers who want to use the Java programming language to create applications. 
http://docs.oracle.com/javase/tutorial/index.html 
JSON - JSON at Wikipedia, the free encyclopedia 
https://en.wikipedia.org/wiki/JSON 
Free Java Download - Download Java for your desktop computer now! 
https://www.java.com/en/download/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值