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 数据类型

Type Description
Number double- precision floating-point format in JavaScript
String double-quoted Unicode with backslash escaping
Boolean true or false
Array an ordered sequence of values
Value it can be a string, a number, true or false, null etc
Object an unordered collection of key:value pairs
Whitespace can be used between any pair of tokens
null empty

JSON Schema

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

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

Keywords Description
$schema The $schema keyword states that this schema is written according to the draft v4 specification.
title You will use this to give a title to your schema.
description A little description of the schema.
type The type keyword defines the first constraint on our JSON data: it has to be a JSON Object.
properties Defines various keys and their value types, minimum and maximum values to be used in JSON file.
required This keeps a list of required properties.
minimum This is the constraint to be put on the value and represents minimum acceptable value.
exclusiveMinimum If “exclusiveMinimum” is present and has boolean value true, the instance is valid if it is strictly greater than the value of “minimum”.
maximum This is the constraint to be put on the value and represents maximum acceptable value.
exclusiveMaximum If “exclusiveMaximum” is present and has boolean value true, the instance is valid if it is strictly lower than the value of “maximum”.
multipleOf A numeric instance is valid against “multipleOf” if the result of the division of the instance by this keyword’s value is an integer.
maxLength The length of a string instance is defined as the maximum number of its characters.
minLength The length of a string instance is defined as the minimum number of its characters.
pattern A 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 实体的对比

JSON JAVA
string java.lang.String
number java.lang.Number
true,false java.lang.Boolean
null null
array java.util.List
object java.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/

猜你喜欢

转载自wangyuxxx.iteye.com/blog/2342487