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 ...
随机推荐
- C#打开SDE数据库的几种方式总结
转自谢灿软件原文 C#打开SDE数据库的几种方式总结 1.通过指定连接属性参数打开数据库 /// <param name="server">数据库服务器名</pa ...
- Vue 学习记录<2>
一.Vue https://vue-loader.vuejs.org/zh-cn/ https://vuejs-templates.github.io/webpack/structure.html
- nginx学习十一 nginx启动流程
今天用了一天的时间看nginx的启动流程,流程还是非常复杂.基本的函数调用有十几个之多.通过看源代码和上网查资料,弄懂了一些函数.有些函数还在学习中,有些函数还待日后学习,这里记录一下今天所学.加油! ...
- js--27门面模式
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- eclipse个人插件
1.SVN eclipse markets 安装m2e-subversion.svnkit 2.maven 本地装好mvn prefences导入maven安装目录和配置 3.单元测试覆盖率 EclE ...
- shell-手机屏幕录制
今天在做android联系的时候,想要把自己写的demo效果记录下来.在网上发现了录制手机屏幕的方法,如下 adb shell screenrecord /sdcard/demo.mp4 解释 adb ...
- Mysql基本增删改查
1登陆服务器 mysql -h localhost -u username -p password 2查看存在数据库 show databases; 3创建一个数据库(例如名字为class1,以下都是 ...
- .Net Standard和各平台关系
.NET Standard 1.0 1.1 1.2 1.3 1.4 1.5 1.6 2.0 .NET 核心 1.0 1.0 1.0 1.0 1.0 1 ...
- apache+nginx 实现动静分离
apache+nginx 实现动静分离 http://blog.csdn.net/xysoul/article/details/50347971
- [Angular] Update FormArray with patchValue
Currently, patchValue doesn't support update FormArray. The workarround is you need to empty the for ...