Json使用

轻量级的数据交换格式Json(JavaScript Object Notation),扩展性很强,并方便支持众多主流语言的操作,所以被广泛应用。

  当需要表示一组值时,JSON 不但能够提高可读性,而且可以减少复杂性。

  如果使用 JSON,就只需将多个带花括号的记录分组在一起:

  var people =

  { "programmers": [ { "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },

  { "firstName": "Jason", "lastName":"Hunter", "email": "bbbb" },

    { "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }

  ],

  "authors": [

  { "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },

  { "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },

  { "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }

  ],

  "musicians": [

  { "firstName": "Eric", "lastName": "Clapton", "instrument": "guitar" },

  { "firstName": "Sergei", "lastName": "Rachmaninoff", "instrument": "piano" }

  ] }

也是可以作为嵌套的方式进行处理,知道如何表示之后,可以通过JS对其进行访问,由于JS是原生态支持Json的,所以完全处理非常简便。
  访问
  var name = people.programmers[0].lastName;
   
  修改:
  people.programmers[0].lastName = "Smith";
  
  转换:
  Json to Str :String newJSONtext = people.toJSONString();
  
  Str to Json:   var obj = eval('(' + str + ')');
  使用Java处理Json
 1 import java.util.HashMap;
 2 import java.util.Map;
 3 
 4 import net.sf.json.JSONObject;
 5 
 6 public class Test {
 7 
 8   public static void main(String[] args) {
 9   String json = "{\"name\":\"reiz\"}";
10   JSONObject jsonObj = JSONObject.fromObject(json);
11   String name = jsonObj.getString("name");
12     
13   jsonObj.put("initial", name.substring(0, 1).toUpperCase());
14 
15   String[] likes = new String[] { "JavaScript", "Skiing", "Apple Pie" };
16   jsonObj.put("likes", likes);
17 
18   Map<String, String> ingredients = new HashMap<String, String>();
19   ingredients.put("apples", "3kg");
20   ingredients.put("sugar", "1kg");
21   ingredients.put("pastry", "2.4kg");
22   ingredients.put("bestEaten", "outdoors");
23   jsonObj.put("ingredients",ingredients);
24    
25   System.out.println(jsonObj);
26   }
27 }

  下面是使用Json的程序

  

 1 import java.util.HashMap;
 2 import java.util.Map;
 3 
 4 import org.json.JSONException;
 5 import org.json.JSONObject;
 6 
 7 public class Test {
 8 
 9   public static void main(String[] args) throws JSONException {
10   String json = "{\"name\":\"reiz\"}";
11   JSONObject jsonObj = new JSONObject(json);
12   String name = jsonObj.getString("name");
13 
14   jsonObj.put("initial", name.substring(0, 1).toUpperCase());
15 
16   String[] likes = new String[] { "JavaScript", "Skiing", "Apple Pie" };
17   jsonObj.put("likes", likes);
18 
19   Map<String, String> ingredients = new HashMap<String, String>();
20   ingredients.put("apples", "3kg");
21   ingredients.put("sugar", "1kg");
22   ingredients.put("pastry", "2.4kg");
23   ingredients.put("bestEaten", "outdoors");
24   jsonObj.put("ingredients", ingredients);
25   System.out.println(jsonObj);
26 
27   System.out.println(jsonObj);
28   }
29 }

两者的使用几乎是相同的,但org.json比json-lib要轻量得多,前者没有任何依赖,而后者要依赖ezmorph和commons的lang、logging、beanutils、collections等组件。

 
  参考资料:
    http://baike.baidu.com/view/136475.htm
    http://www.lisdn.com/html/22/n-9522.html
posted @ 2013-01-04 16:24  曦之易夕  阅读(175)  评论(0编辑  收藏  举报