一。JavaProject中org.json解析JSON

1.JSON的org.son-api下载

一般搜索jar使用即可。

1)JSON网址

http://www.json.org/

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


  1. import org.json.JSONArray;
  2. import org.json.JSONException;
  3. import org.json.JSONObject;

1)String <---> JSON


  1. /** String转JSON(JSONArray) **/
  2. // 最外围由 [] 包裹
  3. String jsonStr1 = "[{ " + " \"user3\" : \"翠花\", "
  4. + " \"pswd3\" : \"cuihua123\", "
  5. + " \"body\": { \"chest\": 68, \"waist\": 65, \"hip\": 68 }"
  6. + "}]";
  7. JSONArray jarray2 = new JSONArray(jsonStr1);
  8. System.out.println(jarray2.getJSONObject(0));
  9. /** String 转换成为 JSON(JSONObject) 对象 */
  10. // 最外围由 {} 包裹
  11. String jsonStr2 = "{ " + " \"user4\" :\"凤姐\", "
  12. + " \"pswd4\" :\"fengjie123\", "
  13. + " \"body\": { \"chest\":65, \"waist\":63, \"hip\":66 }" + "}";
  14. JSONObject jsonObj1 = new JSONObject(jsonStr2);
  15. // 根据属性名称获取String型数据;
  16. String user4 = jsonObj1.getString("user4");
  17. String pswd4 = jsonObj1.getString("pswd4");
  18. // 根据属性名获取JSONObject类
  19. JSONObject body = jsonObj1.getJSONObject("body");
  20. // 根据属性名称获取int型数据;
  21. int chest = body.getInt("chest");
  22. int waist = body.getInt("waist");
  23. int hip = body.getInt("hip");

2)集合 <---> JSON


  1. /** List集合转JSON(JSONArray) **/
  2. Map<String, String> param1 = new HashMap<String, String>();
  3. param1.put("user1", "小白");
  4. param1.put("pswd1", "xiaobai123");
  5. Map<String, String> param2 = new HashMap<String, String>();
  6. param2.put("user2", "小黑");
  7. param2.put("pswd2", "xiaohei123");
  8. // JSONArray initial value should be a string or collection or array.
  9. List<Object> params = new ArrayList<Object>();
  10. params.add(param1);
  11. params.add(param2);
  12. // 声明JSONArray对象并输入JSON字符串
  13. JSONArray jarray1 = new JSONArray(params); // 除了构造,还有put方法
  14. System.out.println(jarray1.toString());
  15. /** Map 转 JSON **/
  16. Map<String, String> map = new HashMap<String, String>();
  17. map.put("user5", "小五");
  18. map.put("pswd5", "xiaowu123");
  19. JSONArray jarray3 = new JSONArray();
  20. jarray3.put(map); // 可以put进map,或者list
  21. System.out.println(jarray3.toString());

3)JavaBean <---> JSON


  1. /* JavaBean的属性:
  2. String username;
  3. String password;
  4. int chest, waist, hip; */
  5. /** JavaBean 转换成为 JSON(JSONObject) 对象 */
  6. bean.setUsername("小四");
  7. JSONObject jsonObj2 = new JSONObject(bean);
  8. System.out.println(jsonObj2.get("username") + ", chest:" + jsonObj2.getInt("chest"));

二。JavaProject中json-lib解析JSON

1.JSON的json-lib-api下载

http://www.json.org/

http://json-lib.sourceforge.net/

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

2.在JavaProject中使用

需要其它依赖jar,有些麻烦。

3.使用json-lib解析JSON


  1. import net.sf.json.JSONArray;
  2. import net.sf.json.JSONObject;

1)String <---> JSON


  1. // 一般数据 转 JSON(JSONArray)
  2. JSONArray jsonArray3 = JSONArray.fromObject("[{'user3':'凤姐'}, {'pswd3':'fengjie123'}]" );
  3. System.out.println(jsonArray3.toString());
  4. // 一般数据 转 JSON(JSONArray)
  5. JSONArray jsonArray4 = JSONArray.fromObject("['user4', '凤姐xxx', 'pswd4', 'fengjiexxx123']" );
  6. System.out.println(jsonArray4.toString());
  7. // 数组 转 JSON(JSONArray)
  8. String[] strs = new String[]{"小白", "小黑", "小四", "岳父"};
  9. JSONArray jsonArray2 = JSONArray.fromObject(strs);
  10. System.out.println(jsonArray2.toString());

2)集合 <---> JSON


  1. // List 集合转 JSON(JSONArray)
  2. List list1 = new ArrayList();
  3. list1.add("user1");
  4. list1.add("pswd1");
  5. JSONArray jsonArray1 = JSONArray.fromObject(list1);
  6. System.out.println(jsonArray1.toString());
  7. // Map 集合转 JSON(JSONObject)
  8. Map<String, String> map = new HashMap<String, String>();
  9. map.put("user2", "芙蓉");
  10. map.put("pswd2", "furong123");
  11. JSONObject jsonObject1 = JSONObject.fromObject(map);
  12. System.out.println(jsonObject1.toString());

3)JavaBean <---> JSON


  1. // Bean 转 JSON(JSONObject)
  2. JSONObject jsonObject2 = JSONObject.fromObject(new JavaBean("拱拱", "gonggong123", 34, 44, 43));
  3. System.out.println(jsonObject2.toString());
  4. // beans 转 JSON
  5. List list2 = new ArrayList();
  6. JavaBean bean1 = new JavaBean("逗比", "doubi123", 56, 54, 55);
  7. JavaBean bean2 = new JavaBean("屌丝", "diaosi123", 34, 45, 56);
  8. list2.add(bean1);
  9. list2.add(bean2);
  10. JSONArray jsonArray5 = JSONArray.fromObject(list2);
  11. System.out.println(jsonArray5.toString());

三。AndroidProject中的JSON解析

Android中JSON解析器使用的是org.json。

Java中使用org.json和json-lib解析JSON的更多相关文章

  1. 通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来的Json数据写入数据库表中

    摘自:http://blog.csdn.net/mazhaojuan/article/details/8592015 通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来 ...

  2. Java中有关构造函数的一道笔试题解析

    Java中有关构造函数的一道笔试题解析 1.详细题目例如以下 下列说法正确的有() A. class中的constructor不可省略 B. constructor必须与class同名,但方法不能与c ...

  3. python爬虫简单实现,并在java中调用python脚本,将数据保存在json文件中

    # coding:utf-8 import urllib2 from bs4 import BeautifulSoup import json import sys reload(sys) sys.s ...

  4. 《项目经验》--通过js获取前台数据向一般处理程序传递Json数据,并解析Json数据,将前台传来的Json数据写入数据库表中

      先看一下我要实现的功能界面:   这个界面的功能在图中已有展现,课程分配(教师教授哪门课程)在之前的页面中已做好.这个页面主要实现的是授课,即给老师教授的课程分配学生.此页面实现功能的步骤已在页面 ...

  5. 使用jQuery解析JSON数据(由ajax发送请求到php文件处理数据返回json数据,然后解析json写入html中呈现)

    在上一篇的Struts2之ajax初析中,我们得到了comments对象的JSON数据,在本篇中,我们将使用jQuery进行数据解析. 我们先以解析上例中的comments对象的JSON数据为例,然后 ...

  6. 18 Ajax、Json以及jackson框架解析json的基本应用

    1. Ajax (1)概念:ASynchronous JavaScript And XML 异步的JavaScript 和 XML 异步和同步:客户端和服务器端相互通信的基础上 * 客户端必须等待服务 ...

  7. php怎么解析utf-8带BOM编码的json数据,php解析json数据返回NULL

    今天遇到一个问题,json_decode解析json数据返回null,试了各种方法都不行,最后发现,原来是json文件编码的问题. 当json_decode解析utf-8带BOM格式的json数据时, ...

  8. javascript如何解析json对javascript如何解析json对象并动态赋值到select列表象并动态赋值到select列表

    原文 javascript如何解析json对象并动态赋值到select列表 JSON(JavaScriptObject Notation)一种简单的数据格式,比xml更轻巧.JSON是JavaScri ...

  9. C# JavaScriptSerializer 解析Json数据(多方法解析Json 三)

    准备工作: 1.添加引用System.Web.Extensions, 2..net3.5+版本都有,如果VS2010找不到,在这个文件夹找:C:\Program Files\Reference Ass ...

  10. java中使用fastjson、jackson、json-lib解析JSON-------------------妈妈再也不用担心JSON解析

    1.fastjson引入包<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjso ...

随机推荐

  1. C#打开SDE数据库的几种方式总结

    转自谢灿软件原文 C#打开SDE数据库的几种方式总结 1.通过指定连接属性参数打开数据库 /// <param name="server">数据库服务器名</pa ...

  2. Vue 学习记录<2>

    一.Vue https://vue-loader.vuejs.org/zh-cn/ https://vuejs-templates.github.io/webpack/structure.html

  3. nginx学习十一 nginx启动流程

    今天用了一天的时间看nginx的启动流程,流程还是非常复杂.基本的函数调用有十几个之多.通过看源代码和上网查资料,弄懂了一些函数.有些函数还在学习中,有些函数还待日后学习,这里记录一下今天所学.加油! ...

  4. js--27门面模式

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  5. eclipse个人插件

    1.SVN eclipse markets 安装m2e-subversion.svnkit 2.maven 本地装好mvn prefences导入maven安装目录和配置 3.单元测试覆盖率 EclE ...

  6. shell-手机屏幕录制

    今天在做android联系的时候,想要把自己写的demo效果记录下来.在网上发现了录制手机屏幕的方法,如下 adb shell screenrecord /sdcard/demo.mp4 解释 adb ...

  7. Mysql基本增删改查

    1登陆服务器 mysql -h localhost -u username -p password 2查看存在数据库 show databases; 3创建一个数据库(例如名字为class1,以下都是 ...

  8. .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 ...

  9. apache+nginx 实现动静分离

    apache+nginx 实现动静分离 http://blog.csdn.net/xysoul/article/details/50347971

  10. [Angular] Update FormArray with patchValue

    Currently, patchValue doesn't support update FormArray. The workarround is you need to empty the for ...