json代替xml的好东西。

    技术2022-05-11  57

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

    JSON建构于两种结构:

    “名称/值”对的集合(A collection of name/value pairs)。不同的语言中,它被理解为对象(object),纪录(record),结构(struct),字典(dictionary),哈希表(hash table),有键列表(keyed list),或者关联数组 (associative array)。 值的有序列表(An ordered list of values)。在大部分语言中,它被理解为数组(array)。

    这些都是常见的数据结构。事实上大部分现代计算机语言都以某种形式支持它们。这使得一种数据格式在同样基于这些结构的编程语言之间交换成为可能。

    JSON具有以下这些形式:

    对象是一个无序的“‘名称/值’对”集合。一个对象以“{”(左括号)开始,“}”(右括号)结束。每个“名称”后跟一个“:”(冒号);“‘名称/值’ 对”之间使用“,”(逗号)分隔。格式:{"phone":"123456","zip":"7890"}

    数组是值(value)的有序集合。一个数组以“[”(左中括号)开始,“]”(右中括号)结束。值之间使用“,”(逗号)分隔。格式:["hello",123,false,null,123.45,{"phone":"123456","zip":"7890"}]

    (value)可以是双引号括起来的字符串(string)、数值(number)、true、false、 null、对象(object)或者数组(array)。这些结构可以嵌套。

    字符串(string)是由双引号包围的任意数量Unicode字符的集合,使用反斜线转义。一个字符(character)即一个单独的字符串(character string)。

    字符串(string)与C或者Java的字符串非常相似。

    数值(number)也与C或者Java的数值非常相似。除去未曾使用的八进制与十六进制格式。除去一些编码细节。

     

     

     

    json与xml的对比:<contact>    <friend>        <name>Michaelname>        <email>17bity@gmail.comemail>        <homepage>http://www.jialing.nethomepage>    friend>    <friend>        <name>Johnname>        <email>john@gmail.comemail>        <homepage>http://www.john.comhomepage>    friend>    <friend>        <name>Peggyname>        <email>peggy@gmail.comemail>        <homepage>http://www.peggy.comhomepage>    friend>contact>[  {   name:"Michael",   email:"17bity@gmail.com",   homepage:"http://www.jialing.net" }, {   name:"John",   email:"john@gmail.com",   homepage:"http://www.jobn.com" },  {   name:"Peggy",   email:"peggy@gmail.com",   homepage:"http://www.peggy.com" }]

    处理json的一段代码,使用了http://www.json.org官方网站提供的java类文件进行的处理

    package  org.test; import  org.json.JSONArray; import  org.json.JSONException; import  org.json.JSONObject; /** * @author yuyongpeng  * 测试json */ class  TestJson  {    // ["org.bean.testBean@edc3a2","org.bean.testBean@1c6f579","org.bean.testBean@11ddcde","org.bean.testBean@18fb1f7"]    public static void main(String args[]) {        JSONObject obj = new JSONObject();        try {            obj.put("name""foo");            obj.put("num"new Integer(100));            obj.put("balance"new Double(1000.21));            obj.put("is_vip"new Boolean(true));        } catch (JSONException e) {            e.printStackTrace();        }        //{"num":100,"balance":1000.21,"is_vip":true,"name":"foo"}        System.out.println(obj);        JSONObject obj2 = new JSONObject();        try {            obj2.put("phone""123456");            obj2.put("zip""7890");            obj.put("contact", obj2);        } catch (JSONException e) {            e.printStackTrace();        }        //{"num":100,"contact":{"phone":"123456","zip":"7890"},"balance":1000.21,"is_vip":true,"name":"foo"}        System.out.println(obj);        JSONArray array = new JSONArray();        array.put("hello");        array.put(new Integer(123));        array.put(new Boolean(false));        array.put(obj);        array.put(new Double(123.45));        array.put(obj2);// see above        //["hello",123,false,{"num":100,"contact":{"phone":"123456","zip":"7890"},"balance":1000.21,"is_vip":true,"name":"foo"},123.45,{"phone":"123456","zip":"7890"}]        System.out.println(array);        String jsonString = "["hello",123,false,{"num":100,"contact":{"phone":"123456","zip":"7890"},"balance":1000.21,"is_vip":true,"name":"foo"},123.45,{"phone":"123456","zip":"7890"}]";        try {            JSONArray readJsonString = new JSONArray(jsonString);            String hello = readJsonString.getString(0);            int it = readJsonString.getInt(1);            JSONObject jobj = readJsonString.getJSONObject(3);            int num = jobj.getInt("num");            JSONObject jobj2 = readJsonString.getJSONObject(5);            int zip = jobj2.getInt("zip");            System.out.println("test end;");        } catch (JSONException e) {            e.printStackTrace();        }    }}

    相关的一些网站:http://www.json.org/ http://www.json.org/java/  (json官方网站提供的java代码)http://www.json.org/java/simple.txt  (里面的代码使用的方法已经是老的了)


    最新回复(0)