json格式示例

Posted 希望明天会更好

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了json格式示例相关的知识,希望对你有一定的参考价值。

案例一:

  {key:value,key:value}

class Person{
         String firstname = "张";
         String lastname = "三丰";
         Integer age = 100;
      }
      
      Person p = new Person();
      System.out.println(p.firstname);

转JS:

var person = {"firstname":"张","lastname":"三丰","age":100};
     
     //取出lastname
     alert(person.lastname);
     //取出age
     alert(person.age);

案例二:
       [{key:value,key:value},{key:value,key:value}]

    Person[] persons = new Person[1];
    Person p0 = new Person("张","三丰",100);
    Person p1 = new Person("李","四",25);
    persons[0] = p0;
    persons[1] = p1;
    System.out.println(persons[0].age);
    System.out.println(persons[1].firstname);

 

转JS:

var persons = [
                {"firstname":"张","lastname":"三丰","age":100},
                 {"firstname":"李","lastname":"四","age":25}
               ];
     //取出fistname=李
     alert(persons[1].firstname);
     //取出age=100
     alert(persons[0].age);

案例三:
   {

      "param":[{key:value,key:value},{key:value,key:value}]

   }

var json = {
                 "baobao":[
                           {"name":"yg",age:22,"addr":"南通"},
                           {"name":"gy",age:22,"addr":"徐州"}
                           ]
             };
     //取name = gy
     alert(json.baobao[1].name);
     //取addr = 南通 
     alert(json.baobao[0].addr);
    

案例四:
      {
        "param1":[{key:value,key:value},{key:value,key:value}],
        "param2":[{key:value,key:value},{key:value,key:value}],
        "param3":[{key:value,key:value},{key:value,key:value}]
      }

var json = {
                 "baobao":[
                           {"name":"yg",age:22,"addr":"南通"},
                           {"name":"gy",age:22,"addr":"徐州"}
                           ],
                 "haohao":[{"name":"xh",age:23,"addr":"南京"},
                           {"name":"aj",age:22,"addr":"淮阴"}    
                           ]
             };
//取name = gy
alert(json.baobao[1].name);
//取addr = 淮阴 
alert(json.haohao[1].addr);

案例五:
      {
        "param1":"value1",
        "param2":{},
        "param3":[{key:value,key:value},{key:value,key:value}]
      }

var json = {
                 "key1":"value1",
                 "key2":{"firstname":"张","lastname":"三丰","age":100},
                "key3":[
                       {"name":"yg",age:22,"addr":"南通"},
                        {"name":"gy",age:22,"addr":"徐州"}                  
                       ]         
     };
     alert(json.key2.firstname);
     alert(json.key3[1].age);

 













以上是关于json格式示例的主要内容,如果未能解决你的问题,请参考以下文章

html JSON-LD审核片段结构化数据示例

Golang实践录:反射reflect的一些研究及代码汇总

PHP JSON格式数据交互实例代码详解

jQuery中读取json文件示例代码

jQuery中读取json文件示例代码

golang的xorm如何将[]map[string][]byte 格式的数据序列化成json输出