JSON解析实例——使用Json-lib

 

JSON解析实例——使用Json-lib

 

Json-lib下载及使用 

  本文介绍用一个类库进行JSON解析。 

  工具下载地址:http://sourceforge.net/projects/json-lib/?source=directory

 

  下载后,有这么三个东东:

  将第一个Jar包导入项目即可用其中的API。

  (具体方法:在项目中建立libs文件夹,将第一个包复制进去,然后在Java Build Path里面Add External JARs…) 

  第二个包是文档,直接右键解压缩之后就会得到html文档,入口是index.html,当然也可以利用工具(jd2chm.exe)将其转换为chm格式这样查看更方便。  

  第三个包是源码,感兴趣或者以后碰到问题了可以看看。

 

附加包:解决 Exception in thread "main" java.lang.NoClassDefFoundError 

  导入了上面的包之后,运行程序会抛出运行时异常:Exception in thread "main" java.lang.NoClassDefFoundError

  搜索之后,才知道还要添加另外的包,我是从这里下载的: 

  http://download.csdn.net/download/guoyongxue/4423006

  如果没有积分,也可以去我的网盘下载:

  http://pan.baidu.com/share/link?shareid=1066468750&uk=2701745266

  也就是说,一共需要这么多包:

 

   需要把它们全部加入libs文件夹,并且把它们添加进Build Path中。

 

程序实例:利用Json-lib生成JSON字符串

   时间关系,代码不再过多解释,项目结构如图:

 

 

其中的类:

package com.shengqishiwind.domain;

public class Person
{
    private int id;
    private String name;
    private String address;

    public Person()
    {
    }

    public Person(int id, String name, String address)
    {
        super();
        this.id = id;
        this.name = name;
        this.address = address;
    }

    public int getId()
    {
        return id;
    }

    public void setId(int id)
    {
        this.id = id;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getAddress()
    {
        return address;
    }

    public void setAddress(String address)
    {
        this.address = address;
    }

    @Override
    public String toString()
    {
        return "Person [id=" + id + ", name=" + name + ", address=" + address
                + "]";
    }

}
Person.java
package com.shengqishiwind.tools;

import net.sf.json.JSONObject;

public class JsonTools
{

    public JsonTools()
    {

    }

    public static String createJsonString(String key, Object value)
    {
        String jsonString = null;

        JSONObject jsonObject = new JSONObject();
        jsonObject.put(key, value);
        jsonString = jsonObject.toString();

        return jsonString;

    }

}
package com.shengqishiwind.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.shengqishiwind.domain.Person;

public class JsonService
{
    public JsonService()
    {
    }

    public Person getPerson()
    {
        Person person = new Person(001, "ZhangSan", "Beijing");

        return person;

    }

    public List<Person> getPersonsList()
    {
        List<Person> personsList = new ArrayList<Person>();
        Person person1 = new Person(001, "ZhangSan", "Beijing");
        Person person2 = new Person(002, "LiSi", "Tianjin");
        Person person3 = new Person(003, "WangWu", "Shanghai");

        personsList.add(person1);
        personsList.add(person2);
        personsList.add(person3);

        return personsList;
    }

    public List<String> getStringsList()
    {
        List<String> stringsList = new ArrayList<String>();

        stringsList.add("南京");
        stringsList.add("杭州");
        stringsList.add("承德");
        stringsList.add("哈尔滨");
        stringsList.add("重庆");

        return stringsList;
    }

    public List<Map<String, Object>> getMapsList()
    {
        List<Map<String, Object>> mapsList = new ArrayList<Map<String, Object>>();

        for (int i = 0; i < 3; ++i)
        {
            Map<String, Object> map = new HashMap<String, Object>();

            map.put("id", i);
            map.put("name", "Name_" + i);
            map.put("address", "Address_" + i);

            mapsList.add(map);
        }

        return mapsList;

    }

}

  最后测试的主程序:

package com.shengqishiwind.jsontest;

import com.shengqishiwind.domain.Person;
import com.shengqishiwind.service.JsonService;
import com.shengqishiwind.tools.JsonTools;

public class Test1
{

    public static void main(String[] args)
    {
        String msg = "";

        JsonService service = new JsonService();
        Person person = service.getPerson();

        // 一个Person对象
        msg = JsonTools.createJsonString("aPerson", person);
        System.out.println(msg);

        // 一个List,里面有若干Person对象
        msg = JsonTools.createJsonString("Persons", service.getPersonsList());
        System.out.println(msg);

        // 一个List,里面有若干String对象
        msg = JsonTools.createJsonString("StringsList",
                service.getStringsList());
        System.out.println(msg);

        // 一个List,里面有若干Map对象,每个Map对象中存储了若干个键值对
        msg = JsonTools.createJsonString("MapsList", service.getMapsList());
        System.out.println(msg);

    }

}

  程序在控制台输出如下:

{"aPerson":{"address":"Beijing","id":1,"name":"ZhangSan"}}
{"Persons":[{"address":"Beijing","id":1,"name":"ZhangSan"},{"address":"Tianjin","id":2,"name":"LiSi"},{"address":"Shanghai","id":3,"name":"WangWu"}]}
{"StringsList":["南京","杭州","承德","哈尔滨","重庆"]}
{"MapsList":[{"id":0,"address":"Address_0","name":"Name_0"},{"id":1,"address":"Address_1","name":"Name_1"},{"id":2,"address":"Address_2","name":"Name_2"}]}

 

 

 

参考资料

  老罗Android开发视频教程。

  本博客上一篇博文:JSON数据解析 基础知识及链接收集

  Json-lib工具类库下载地址:

   http://sourceforge.net/projects/json-lib/?source=directory

   http://sourceforge.net/projects/json-lib/files/json-lib/json-lib-2.4/

  附加包下载地址:

   http://download.csdn.net/download/guoyongxue/4423006

  我的百度网盘中全部Json-lib资源下载:

   http://pan.baidu.com/share/link?shareid=1066468750&uk=2701745266

 

   如果觉得有帮助,就点个推荐吧~~~

posted @ 2013-06-17 23:08  圣骑士wind  阅读(15321)  评论(1编辑  收藏  举报