码迷,mamicode.com
首页 > 编程语言 > 详细

Java解析(读取)Json数据

时间:2014-04-29 13:11:20      阅读:423      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   java   color   使用   

以前看过书上说,XML是web service间传输信息的标准格式吧,就看了看XML。最近在做个网站,又说是有了JSON,第一回听说就看了看,总结总结一下。

1.JSON介绍

  JSON比XML简单,主要体现在传输相同信息的情况下,文件的大小不同

  JSON只用于传输信息,XML还可以用于配置文件的使用。

  JSON中的符号主要有: " , [ {:

2.JSON中的数组和对象

  2.1数组(JSONArray

  数组用一对[],表示存放的是一般的数组数据。

  如:["11","22","33"],表示这是一个JSONArray数组,里面有3个数据:”11“,”22“,”33“。当然可以是复杂的数据,就是所谓的嵌套定义吧。

  2.对象(JSONObject

  对象用一对{},来表示其中JSON通用的”键-值“对。

  如:{"sex1":"female","name0":"zhangsan"},表示的是一个JSON对象,里面有两组数据(键值对),sex1=female,name0=zhangsan。当然这里键对应的数据值,可以是复杂的JSON对象或者数组。

3.代码示例

3.1基本的JSONArray与JSONObject操作

mamicode.com,码迷
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class ObjectAndArray1 {
    public static void main(String args[])
    {
        JSONObject jsonObj  = new JSONObject();
        jsonObj.put("name0", "zhangsan");
        jsonObj.put("sex1", "female");
        System.out.println(jsonObj);    //输出为:{"sex1":"female","name0":"zhangsan"}
        
        JSONArray jsonArray = new JSONArray();
        jsonArray.add("11");
        jsonArray.add("22");
        jsonArray.add("33");
        System.out.println(jsonArray);    //输出为:["11","22","33"]
    }
}
mamicode.com,码迷

3.2由java自带的数据结构转换为JSON文本

mamicode.com,码迷
import java.util.HashMap;
import java.util.Map;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class ObjectAndArray2{
    public static void main(String args[])
    {
        //可以由数组,列表等生成JSONArray
        String list[]={"11","22"};
        JSONArray jsonarray = JSONArray.fromObject(list); 
        jsonarray.add("33");
        System.out.println(jsonarray);    //输出为:["11","22","33"]
        
        //可以由Map生成JSONObject
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("NO1", "第一个");
        map.put("NO2", "第二个");
        map.put("NO3", jsonarray);
        JSONObject jsonObj  = JSONObject.fromObject(map);
        System.out.println(jsonObj);    //输出为:{"NO3":["11","22","33"],"NO2":"第二个","NO1":"第一个"}
    }
}
mamicode.com,码迷

3.3读取JSON文本

JSONArray必须用下标读取内部数据。
JSONObject必须用”键“读取对应的”值“。
mamicode.com,码迷
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class ObjectAndArray {
    public static void main(String args[])
    {
        JSONArray jsonarray;
        JSONObject jsonObj;
        //读取JSONArray,用下标索引获取
        String array="[\"11\",\"22\",\"33\"]";
        jsonarray = JSONArray.fromObject(array); 
        System.out.println(jsonarray.getString(1));    //输出为:22
        
        //读取JSONObject
        String object="{\"NO1\":[\"44\",\"55\",\"66\"],\"NO2\":{\"NO1\":\"第一个\"}}";
        jsonObj  = JSONObject.fromObject(object);
        System.out.println(jsonObj.get("NO1"));    //输出为:["44","55","66"]
        
        jsonarray = (JSONArray)(jsonObj.get("NO1")); 
        System.out.println(jsonarray.getString(1));    //输出为:55
        
        //用"键"获取值
        jsonObj=(JSONObject)jsonObj.get("NO2");
        System.out.println(jsonObj);    //输出为:{"NO1":"第一个"}
        
    }
}
mamicode.com,码迷

Java解析(读取)Json数据,码迷,mamicode.com

Java解析(读取)Json数据

标签:style   blog   http   java   color   使用   

原文地址:http://blog.csdn.net/u014724587/article/details/24703475

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!