Java中使用org.json和json-lib解析JSON
一。JavaProject中org.json解析JSON
1.JSON的org.son-api下载
一般搜索jar使用即可。
1)JSON网址
2)JSON的java解析org.json-api网址
http://www.json.org/java/index.html
3)JSON的org.json-api源码地址
https://github.com/douglascrockford/JSON-java
本例下载日期:2014-11-17
2.在JavaProject中使用
1)创建org.json包

2)从下载的zip包中解压源码
如果是使用jar,当然没这么麻烦。
3.使用org.json解析JSON
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
1)String <---> JSON
- /** String转JSON(JSONArray) **/
- // 最外围由 [] 包裹
- String jsonStr1 = "[{ " + " \"user3\" : \"翠花\", "
- + " \"pswd3\" : \"cuihua123\", "
- + " \"body\": { \"chest\": 68, \"waist\": 65, \"hip\": 68 }"
- + "}]";
- JSONArray jarray2 = new JSONArray(jsonStr1);
- System.out.println(jarray2.getJSONObject(0));
- /** String 转换成为 JSON(JSONObject) 对象 */
- // 最外围由 {} 包裹
- String jsonStr2 = "{ " + " \"user4\" :\"凤姐\", "
- + " \"pswd4\" :\"fengjie123\", "
- + " \"body\": { \"chest\":65, \"waist\":63, \"hip\":66 }" + "}";
- JSONObject jsonObj1 = new JSONObject(jsonStr2);
- // 根据属性名称获取String型数据;
- String user4 = jsonObj1.getString("user4");
- String pswd4 = jsonObj1.getString("pswd4");
- // 根据属性名获取JSONObject类
- JSONObject body = jsonObj1.getJSONObject("body");
- // 根据属性名称获取int型数据;
- int chest = body.getInt("chest");
- int waist = body.getInt("waist");
- int hip = body.getInt("hip");
2)集合 <---> JSON
- /** List集合转JSON(JSONArray) **/
- Map<String, String> param1 = new HashMap<String, String>();
- param1.put("user1", "小白");
- param1.put("pswd1", "xiaobai123");
- Map<String, String> param2 = new HashMap<String, String>();
- param2.put("user2", "小黑");
- param2.put("pswd2", "xiaohei123");
- // JSONArray initial value should be a string or collection or array.
- List<Object> params = new ArrayList<Object>();
- params.add(param1);
- params.add(param2);
- // 声明JSONArray对象并输入JSON字符串
- JSONArray jarray1 = new JSONArray(params); // 除了构造,还有put方法
- System.out.println(jarray1.toString());
- /** Map 转 JSON **/
- Map<String, String> map = new HashMap<String, String>();
- map.put("user5", "小五");
- map.put("pswd5", "xiaowu123");
- JSONArray jarray3 = new JSONArray();
- jarray3.put(map); // 可以put进map,或者list
- System.out.println(jarray3.toString());
3)JavaBean <---> JSON
- /* JavaBean的属性:
- String username;
- String password;
- int chest, waist, hip; */
- /** JavaBean 转换成为 JSON(JSONObject) 对象 */
- bean.setUsername("小四");
- JSONObject jsonObj2 = new JSONObject(bean);
- System.out.println(jsonObj2.get("username") + ", chest:" + jsonObj2.getInt("chest"));
二。JavaProject中json-lib解析JSON
1.JSON的json-lib-api下载
http://json-lib.sourceforge.net/
http://sourceforge.net/projects/json-lib/files/
2.在JavaProject中使用
需要其它依赖jar,有些麻烦。
3.使用json-lib解析JSON
- import net.sf.json.JSONArray;
- import net.sf.json.JSONObject;
1)String <---> JSON
- // 一般数据 转 JSON(JSONArray)
- JSONArray jsonArray3 = JSONArray.fromObject("[{'user3':'凤姐'}, {'pswd3':'fengjie123'}]" );
- System.out.println(jsonArray3.toString());
- // 一般数据 转 JSON(JSONArray)
- JSONArray jsonArray4 = JSONArray.fromObject("['user4', '凤姐xxx', 'pswd4', 'fengjiexxx123']" );
- System.out.println(jsonArray4.toString());
- // 数组 转 JSON(JSONArray)
- String[] strs = new String[]{"小白", "小黑", "小四", "岳父"};
- JSONArray jsonArray2 = JSONArray.fromObject(strs);
- System.out.println(jsonArray2.toString());
2)集合 <---> JSON
- // List 集合转 JSON(JSONArray)
- List list1 = new ArrayList();
- list1.add("user1");
- list1.add("pswd1");
- JSONArray jsonArray1 = JSONArray.fromObject(list1);
- System.out.println(jsonArray1.toString());
- // Map 集合转 JSON(JSONObject)
- Map<String, String> map = new HashMap<String, String>();
- map.put("user2", "芙蓉");
- map.put("pswd2", "furong123");
- JSONObject jsonObject1 = JSONObject.fromObject(map);
- System.out.println(jsonObject1.toString());
3)JavaBean <---> JSON
- // Bean 转 JSON(JSONObject)
- JSONObject jsonObject2 = JSONObject.fromObject(new JavaBean("拱拱", "gonggong123", 34, 44, 43));
- System.out.println(jsonObject2.toString());
- // beans 转 JSON
- List list2 = new ArrayList();
- JavaBean bean1 = new JavaBean("逗比", "doubi123", 56, 54, 55);
- JavaBean bean2 = new JavaBean("屌丝", "diaosi123", 34, 45, 56);
- list2.add(bean1);
- list2.add(bean2);
- JSONArray jsonArray5 = JSONArray.fromObject(list2);
- System.out.println(jsonArray5.toString());
三。AndroidProject中的JSON解析
Android中JSON解析器使用的是org.json。
Java中使用org.json和json-lib解析JSON的更多相关文章
- 通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来的Json数据写入数据库表中
摘自:http://blog.csdn.net/mazhaojuan/article/details/8592015 通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来 ...
- Java中有关构造函数的一道笔试题解析
Java中有关构造函数的一道笔试题解析 1.详细题目例如以下 下列说法正确的有() A. class中的constructor不可省略 B. constructor必须与class同名,但方法不能与c ...
- python爬虫简单实现,并在java中调用python脚本,将数据保存在json文件中
# coding:utf-8 import urllib2 from bs4 import BeautifulSoup import json import sys reload(sys) sys.s ...
- 《项目经验》--通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来的Json数据写入数据库表中
先看一下我要实现的功能界面: 这个界面的功能在图中已有展现,课程分配(教师教授哪门课程)在之前的页面中已做好.这个页面主要实现的是授课,即给老师教授的课程分配学生.此页面实现功能的步骤已在页面 ...
- 使用jQuery解析JSON数据(由ajax发送请求到php文件处理数据返回json数据,然后解析json写入html中呈现)
在上一篇的Struts2之ajax初析中,我们得到了comments对象的JSON数据,在本篇中,我们将使用jQuery进行数据解析. 我们先以解析上例中的comments对象的JSON数据为例,然后 ...
- 18 Ajax、Json以及jackson框架解析json的基本应用
1. Ajax (1)概念:ASynchronous JavaScript And XML 异步的JavaScript 和 XML 异步和同步:客户端和服务器端相互通信的基础上 * 客户端必须等待服务 ...
- php怎么解析utf-8带BOM编码的json数据,php解析json数据返回NULL
今天遇到一个问题,json_decode解析json数据返回null,试了各种方法都不行,最后发现,原来是json文件编码的问题. 当json_decode解析utf-8带BOM格式的json数据时, ...
- javascript如何解析json对javascript如何解析json对象并动态赋值到select列表象并动态赋值到select列表
原文 javascript如何解析json对象并动态赋值到select列表 JSON(JavaScriptObject Notation)一种简单的数据格式,比xml更轻巧.JSON是JavaScri ...
- C# JavaScriptSerializer 解析Json数据(多方法解析Json 三)
准备工作: 1.添加引用System.Web.Extensions, 2..net3.5+版本都有,如果VS2010找不到,在这个文件夹找:C:\Program Files\Reference Ass ...
- java中使用fastjson、jackson、json-lib解析JSON-------------------妈妈再也不用担心JSON解析
1.fastjson引入包<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjso ...
随机推荐
- JavaScript学习总结(11)——JS常用函数(二)
37. getElementsByClassName ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 function getElementsByClassName( ...
- android关键组件service服务(一)
一. Service简单介绍 Service是android 系统中的四大组件之中的一个(Activity.Service.BroadcastReceiver.ContentProvider),它跟A ...
- eclipse - 下载网址
这里面有着非常齐全的eclipse相关资源,而且都是放在网盘里面的,下载也方便 http://www.androiddevtools.cn/
- 阿姆达尔定律(Amdahl's law)
首先给出阿姆达尔定律的数学公式描述: S(N)=1(1−p)+pN p:程序中可并行部分的程序在单核上执行时间的占比: N:处理器的数目(总的核心数) S(N):程序在 N 个处理器(总核心数)相对在 ...
- Codefroces 812 B. Sagheer, the Hausmeister
http://codeforces.com/problemset/problem/812/B B. Sagheer, the Hausmeister time limit per test 1 sec ...
- Android抖动的EditText
Java代码:启动动画 Animation shake = AnimationUtils.loadAnimation(this, R.anim.shake); findViewById(R.id.pw ...
- OrmLite使用小结(一)
在使用OrmLite过程中,遇到了不少问题.鉴于中文文档比較少,看英文文档又不知道怎样看起.仅仅能遇到问题查找解决方法并整理出来,如有错误,希望能指正! ** 1.模糊条件查询 ** 使用条件查询时. ...
- 一起talk C栗子吧(第九十 三回:C语言实例--进程间通信之临界资源)
各位看官们.大家好,前面章回中咱们说的是使用信号和管道进行进程间通信的样例.这一回咱们说的样例是:进程间通信之临界资源.闲话休提,言归正转.让我们一起talk C栗子吧! 我们首先介绍一下,什么是临界 ...
- 20款PHP版WebMail开源项目
20款PHP版WebMail开源项目 如今互联网巨头提供的企业应用套件中邮件托管是必备服务,而且还始终秉承免费的优良光荣传统,最为让人熟识的恐怕非"瘟多死里屋管理中心"和" ...
- js无缝滚动原理及详解[转自刹那芳华]
刚刚接触JS,网上找了一些关于无缝滚动的教程,但都大同小异,对我这种新手来说也只是会用,不知道什么意思,想要自己写个更是一头雾水.于是找了一些资料,详细说明一下JS无缝滚动的原理,相信看过这篇文章之后 ...

