一、JAR包简介
      要使程序 可以运行 必须引入JSON-lib包,JSON-lib包同时依赖于以下的JAR包:
      1.commons-lang.jar
      2.commons-beanutils.jar
      3.commons-collections.jar
      4.commons-logging.jar 
      5.ezmorph.jar
      6.json-lib-2.2.2-jdk15.jar
      
二、JSONObject对象 使用
     JSON-lib包是一个beans,collections,maps,java arrays 和XML和JSON互相转换的包。在本例中,我们将使用JSONObject类创建JSONObject对象,然后我们打印这些对象的值。为了使用 JSONObject对象,我们要引入"net.sf.json"包。为了给对象添加元素,我们要使用put()方法。

import net.sf.json.JSONArray;
import net.sf.json.JSONObject; public class JSONObjectSample { //创建JSONObject对象
private static JSONObject createJSONObject(){
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", "kevin");
jsonObject.put("Max.score", new Integer(100));
jsonObject.put("Min.score", new Integer(50));
jsonObject.put("nickname", "picglet");
return jsonObject;
}
public static void main(String[] args) {
JSONObject jsonObject = JSONObjectSample.createJSONObject();
//输出jsonobject对象
System.out.println("jsonObject==>"+jsonObject); //判读输出对象的类型
boolean isArray = jsonObject.isArray();
boolean isEmpty = jsonObject.isEmpty();
boolean isNullObject = jsonObject.isNullObject();
System.out.println("isArray:"+isArray+" isEmpty:"+isEmpty+" isNullObject:"+isNullObject); //添加属性
jsonObject.element("address", "swap lake");
System.out.println("添加属性后的对象==>"+jsonObject); //返回一个JSONArray对象
JSONArray jsonArray = new JSONArray();
jsonArray.add(0, "this is a jsonArray value");
jsonArray.add(1,"another jsonArray value");
jsonObject.element("jsonArray", jsonArray);
JSONArray array = jsonObject.getJSONArray("jsonArray");
System.out.println("返回一个JSONArray对象:"+array);
//添加JSONArray后的值
//{"name":"kevin","Max.score":100,"Min.score":50,"nickname":"picglet","address":"swap lake",
//"jsonArray":["this is a jsonArray value","another jsonArray value"]}
System.out.println(jsonObject); //根据key返回一个字符串
String jsonString = jsonObject.getString("name");
System.out.println("jsonString==>"+jsonString);
}
} import net.sf.json.JSONArray;
import net.sf.json.JSONObject; public class JSONObjectSample { //创建JSONObject对象
private static JSONObject createJSONObject(){
JSONObject jsonObject = new JSONObject();
jsonObject.put("name", "kevin");
jsonObject.put("Max.score", new Integer(100));
jsonObject.put("Min.score", new Integer(50));
jsonObject.put("nickname", "picglet");
return jsonObject;
}
public static void main(String[] args) {
JSONObject jsonObject = JSONObjectSample.createJSONObject();
//输出jsonobject对象
System.out.println("jsonObject==>"+jsonObject); //判读输出对象的类型
boolean isArray = jsonObject.isArray();
boolean isEmpty = jsonObject.isEmpty();
boolean isNullObject = jsonObject.isNullObject();
System.out.println("isArray:"+isArray+" isEmpty:"+isEmpty+" isNullObject:"+isNullObject); //添加属性
jsonObject.element("address", "swap lake");
System.out.println("添加属性后的对象==>"+jsonObject); //返回一个JSONArray对象
JSONArray jsonArray = new JSONArray();
jsonArray.add(0, "this is a jsonArray value");
jsonArray.add(1,"another jsonArray value");
jsonObject.element("jsonArray", jsonArray);
JSONArray array = jsonObject.getJSONArray("jsonArray");
System.out.println("返回一个JSONArray对象:"+array);
//添加JSONArray后的值
//{"name":"kevin","Max.score":100,"Min.score":50,"nickname":"picglet","address":"swap lake",
//"jsonArray":["this is a jsonArray value","another jsonArray value"]}
System.out.println(jsonObject); //根据key返回一个字符串
String jsonString = jsonObject.getString("name");
System.out.println("jsonString==>"+jsonString);
}
}

得到JSONObject对象后我们就可以使用它的方法了,可以查看其API,我给出一个在线 的API

http://json-lib.sourceforge.net/apidocs/jdk15/index.html
   
Html代码 
JSONArray的方法测试可以类似进行

JSONArray的方法测试可以类似进行

JSONObject以及json(转)的更多相关文章

  1. 在JAVA中使用JSONObject生成json

    JSON是一种轻量级的数据交换格式,在现在的web开发中,是非常常见的.在没有方便的工具之前,我们或许会使用拼字符串的形式来生成json数组,今天我们使用一个json-lib.jar包来为我们实现生成 ...

  2. JSONObject 转换 JSON复杂对象

    Bean定义: public class GetM100DataResponse { private String service;//接口代码 private String sessionId;// ...

  3. JSONObject解析json数据

    首先先看一下我们要解析的json数据是什么样子的: 代码: String url="http://113.57.190.228:8001/Web/Report/GetBigMSKReport ...

  4. 使用JsonObject解析json

    第一种: [ { "0": "1", "1": "一", "id": "1", ...

  5. Android使用自带JSONObject解析JSON数据

    import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android ...

  6. JSONObject转换JSON之将Date转换为指定格式(转)

    项目中,经常会用JSONObject插件将JavaBean或List<JavaBean>转换为JSON格式的字符串,而JavaBean的属性有时候会有java.util.Date这个类型的 ...

  7. 【Unity】使用JSONObject解析Json

    为何要用JSONObject 之前已经用过JsonUtility和Newton.Json来解析Json了,为什么现在又要用一个新的JSONObject来解析Json? 使用JsonUtility:ht ...

  8. JSONObject JSONArray json字符串 HashMap ArryList 在java开发中用到的数据结构

    1.JSONObject  长成这样的:   { "key1":value1, "key2":value2, "key3":value3} ...

  9. 浅谈JSONObject解析JSON数据

    我们在做jmeter接口测试时能会用beanshell断言,一般都会将返回值转成JSONObject对象进行处理.本文选取较为复杂json格式数据,也将适用于java接口测试. JSON数据 { &q ...

随机推荐

  1. #Git 详细中文安装教程

    Step 1 Information 信息 Please read the following important information before continuing 继续之前,请阅读以下重要 ...

  2. CF455D. Serega and Fun

    D. Serega and Fun time limit per test 4 seconds memory limit per test 256 megabytes input standard i ...

  3. C++ 内存分配(new,operator new)详解

    参考:C++ 内存分配(new,operator new)详解 如何限制对象只能建立在堆上或者栈上 new运算符和operator new() new:指我们在C++里通常用到的运算符,比如A* a ...

  4. Scala入门4(_的用法)

    从网上找了一篇博客,详细讲解了Scala下划线的用法,这里做保留 博客链接

  5. 20169211 《Linux内核原理与分析》第十一周作业

    SET-UID程序漏洞实验 一.实验简介 Set-UID 是Unix系统中的一个重要的安全机制.当一个Set-UID程序运行的时候,它被假设为具有拥有者的权限.例如,如果程序的拥有者是root,那么任 ...

  6. PHP的单引号和双引号

    单引号内部的变量不会执行,双引号会执行. <?php $name = 'hello php'; echo "<h1>$name</h1>"; echo ...

  7. CSU - 2062 Z‘s Array

    Description Z likes to play with array. One day his teacher gave him an array of n elements, and ask ...

  8. 五、django rest_framework源码之版本控制剖析

    1 绪论 Djangorest_framework的版本控制允许用户更改不同客户端之间的行为,且提供了许多不同的版本控制方案.版本控制由传入的客户端请求确定,可以基于请求URL,也可以基于请求标头. ...

  9. Bzoj2164 采矿(线段树+树链剖分)

    题面 Bzoj 题解 对于每个节点,我们可以用树链剖分和线段树维护以下信息: 单独在某个点分配\(i\)个人的最大收益(可以\(O(m)\)计算) 分配\(i\)的最大收益(可以\(O(m^2)\)计 ...

  10. 美团外卖iOS多端复用的推动、支撑与思考

    背景 美团外卖2013年11月开始起步,随后高速发展,不断刷新多项行业记录.截止至2018年5月19日,日订单量峰值已超过2000万,是全球规模最大的外卖平台.业务的快速发展对技术支撑提出了更高的要求 ...