JSON —— org.json包使用测试

JSON —— org.json包使用测试

package org.json;

import junit.framework.TestCase;

p lic class Test extends TestCase {

 /*{
 "JSONArray": [],
 "JSONObject": {},
 "String": "98.6",
 "//
": "/
",
 "//
": "/
",
 "bool": "tr",
 "do le": 1.2345678901234568E29,
 "false": false,
 "foo": [
     tr,
     false,
     9876543210,
     0,
     1.00000001,
     1.000000000001,
     1,
     1.0E-17,
     2,
     0.1,
     2.0E100,
     -32,
     [],
     {},
     "string",
     666,
     2001.99,
     "so /"fine/".",
     "so <fine>.",
     tr,
     false,
     [],
     {}
 ],
 "int": 57,
 "keys": [
     "foo",
     "op",
     "String",
     "JSONObject",
     "//
",
     "ten",
     "tr",
     "int",
     "bool",
     "//
",
     "to",
     "do le",
     "JSONArray",
     "null",
     "zero",
     "false"
 ],
 "null": null,
 "op": "Good",
 "ten": 10,
 "to": null,
 "tr": tr,
 "zero": -0
 }
 String: 98.6
 bool: tr
 to: null
 tr: tr
 foo: [tr,false,9876543210,0,1.00000001,1.000000000001,1,1.0E-17,2,0.1,2.0E100,-32,[],{},"string",666,2001.99,"so /"fine/".","so <fine>.",tr,false,[],{}]
 op: Good
 ten: 10
 oops: false
 */
 p lic void testReadJSON() {
  JSONArray jsonArray;
  JSONObject jsonObj;
  try {
   jsonObj = new JSONObject(
     "{foo: [tr, false,9876543210,    0.0, 1.00000001,  1.000000000001, 1.00000000000000001,"
       + " .00000000000000001, 2.00, 0.1, 2e100, -32,[],{}, /"string/"], "
       + "  to   : null, op : 'Good',"
       + "ten:10} postfix comment");

   jsonObj.put("String", "98.6");
   jsonObj.put("JSONObject", new JSONObject());
   jsonObj.put("JSONArray", new JSONArray());
   jsonObj.put("int", 57);
   jsonObj.put("do le", 123456789012345678901234567890.);
   jsonObj.put("tr", tr);
   jsonObj.put("false", false);
   jsonObj.put("null", JSONObject.NULL);
   jsonObj.put("bool", "tr");
   jsonObj.put("zero", -0.0);
   jsonObj.put("//
", "/
");
   jsonObj.put("//
", "/
");
   jsonArray = jsonObj.getJSONArray("foo");
   jsonArray.put(666);
   jsonArray.put(2001.99);
   jsonArray.put("so /"fine/".");
   jsonArray.put("so <fine>.");
   jsonArray.put(tr);
   jsonArray.put(false);
   jsonArray.put(new JSONArray());
   jsonArray.put(new JSONObject());
   jsonObj.put("keys", JSONObject.getNames(jsonObj));
   System.out.println(jsonObj.toString(4));

   System.out.println("String: " + jsonObj.getDo le("String"));
   System.out.println("  bool: " + jsonObj.getBoolean("bool"));
   System.out.println("    to: " + jsonObj.getString("to"));
   System.out.println("  tr: " + jsonObj.getString("tr"));
   System.out.println("   foo: " + jsonObj.getJSONArray("foo"));
   System.out.println("    op: " + jsonObj.getString("op"));
   System.out.println("   ten: " + jsonObj.getInt("ten"));
   System.out.println("  oops: " + jsonObj.optBoolean("oops"));
  } catch (JSONException e) {
   e.printStackTrace();
  }
 }

 /*{
 "Testing JSONString interface": {"A beany object":42},
 "aBoolean": tr,
 "aNumber": 42,
 "aString": "A beany object"
 }
 */
 p lic void testBean() {
  Obj obj = new Obj("A beany object", 42, tr);
  JSONObject jsonObj;
  String sa[] = { "aString", "aNumber", "aBoolean" };
  jsonObj = new JSONObject(obj, sa);
  try {
   jsonObj.put("Testing JSONString interface", obj);
   System.out.println(jsonObj.toString(4));
  } catch (JSONException e) {
   e.printStackTrace();
  }

 }

 /*
  * [1,2,3]
  */
 p lic void testArr() {
  int ar[] = { 1, 2, 3 };
  JSONArray ja;
  try {
   ja = new JSONArray(ar);
   System.out.println(ja.toString());
  } catch (JSONException e) {
   e.printStackTrace();
  }

 }

 /*
 {"single":"MARIE HAA'S","Johnny":"MARIE HAA//'S","foo":"bar","baz":[{"quux":"Thanks, Josh!"}],"obj keys":"11"}
 {"a":[[["b"]]]}
  */
 p lic void testJsonStringer() {
  JSONStringer jsonStringer;
  String str;
  jsonStringer = new JSONStringer();
  try {
   str = jsonStringer.object().key("single").val("MARIE HAA'S").key(
     "Johnny").val("MARIE HAA//'S").key("foo").val("bar")
     .key("baz").array().object().key("quux").val(
       "Thanks, Josh!").endObject().endArray().key(
       "obj keys").val("11").endObject().toString();

   System.out.println(str);

   System.out.println(new JSONStringer().object().key("a").array()
     .array().array().val("b").endArray().endArray()
     .endArray().endObject().toString());
  } catch (JSONException e) {
   e.printStackTrace();
  }
 }

 /*
 {"recipe": {
 "cook_time": "3 hours",
 "ingredient": [
     {
         "amount": 4,
         "content": "Water",
         "state": "warm",
         "unit": "dL"
     },
     {
         "amount": 1,
         "content": "Salt",
         "unit": "teaspoon"
     }
 ],
 "instr tions": {"step": [
     "Knead thoroughly.",
     "Knead again."
 ]},
 "name": "bread",
 "prep_time": "5 mins",
 "title": "Basic bread"
 }}

 {"cook_time":"3 hours","prep_time":"5 mins","childNodes":[{"childNodes":["Basic bread"],"tagName":"title"},{"amount":4,"childNodes":["Water"],"unit":"dL","state":"warm","tagName":"ingredient"},{"amount":1,"childNodes":["Salt"],"unit":"teaspoon","tagName":"ingredient"},{"childNodes":[{"childNodes":["Knead thoroughly."],"tagName":"step"},{"childNodes":["Knead again."],"tagName":"step"}],"tagName":"instr tions"}],"name":"bread","tagName":"recipe"}
 <recipe cook_time="3 hours" prep_time="5 mins" name="bread"><title>Basic bread</title><ingredient amount="4" unit="dL" state="warm">Water</ingredient><ingredient amount="1" unit="teaspoon">Salt</ingredient><instr tions><step>Knead thoroughly.</step><step>Knead again.</step></instr tions></recipe>

 [
 "recipe",
 {
     "cook_time": "3 hours",
     "name": "bread",
     "prep_time": "5 mins"
 },
 [
     "title",
     "Basic bread"
 ],
 [
     "ingredient",
     {
         "amount": 4,
         "state": "warm",
         "unit": "dL"
     },
     "Water"
 ],
 [
     "ingredient",
     {
         "amount": 1,
         "unit": "teaspoon"
     },
     "Salt"
 ],
 [
     "instr tions",
     [
         "step",
         "Knead thoroughly."
     ],
     [
         "step",
         "Knead again."
     ]
 ]
 ]
 <recipe cook_time="3 hours" prep_time="5 mins" name="bread"><title>Basic bread</title><ingredient amount="4" unit="dL" state="warm">Water</ingredient><ingredient amount="1" unit="teaspoon">Salt</ingredient><instr tions><step>Knead thoroughly.</step><step>Knead again.</step></instr tions></recipe>
 */
 p lic void testXML() {
  JSONArray jsonArray;
  JSONObject jsonObj;
  String str;
  str = "<recipe name=/"bread/" prep_time=/"5 mins/" cook_time=/"3 hours/">"
    + " <title>Basic bread</title> "
    + " <ingredient amount=/"4/" unit=/"dL/" state=/"warm/">Water</ingredient> "
    + " <ingredient amount=/"1/" unit=/"teaspoon/">Salt</ingredient> "
    + " <instr tions> "
    + " <step>Knead thoroughly.</step>"
    + " <step>Knead again.</step> "
    + " </instr tions> "
    + "</recipe> ";
  //XML转换成JSON对象
  try {
   jsonObj = XML.toJSONObject(str);

   System.out.println(jsonObj.toString(4));//JSON格式化输出
   System.out.println();

   jsonObj = JSONML.toJSONObject(str);
   System.out.println(jsonObj.toString());
   System.out.println(JSONML.toString(jsonObj));
   System.out.println();

   jsonArray = JSONML.toJSONArray(str);
   System.out.println(jsonArray.toString(4));
   System.out.println(JSONML.toString(jsonArray));
   System.out.println();
  } catch (JSONException e) {
   e.printStackTrace();
  }
 }

 /*
 {"list of lists": [
 [
     1,
     2
 ],
 [
     4,
     5
 ]
 ]}
 <list of lists><array>1</array><array>2</array></list of lists><list of lists><array>4</array><array>5</array></list of lists>
 */
 p lic void testJsonStr() {
  JSONObject jsonObj;
  String str;
  str = "{     /"list of lists/" : [         [1, 2],         [4, 5],     ] }";
  //以JSON串构造JSON对象
  try {
   jsonObj = new JSONObject(str);

   //JSON对象格式化输出
   System.out.println(jsonObj.toString(4));
   //JSON对象到XML的转换
   System.out.println(XML.toString(jsonObj));
  } catch (JSONException e) {
   e.printStackTrace();
  }
 }
}

class Obj implements JSONString {
 p lic String aString;
 p lic do le aNumber;
 p lic boolean aBoolean;

 p lic Obj(String string, do le n, boolean b) {
  this.aString = string;
  this.aNumber = n;
  this.aBoolean = b;
 }

 p lic do le getNumber() {
  return this.aNumber;
 }

 p lic String getString() {
  return this.aString;
 }

 p lic boolean isBoolean() {
  return this.aBoolean;
 }

 p lic String getBENT() {
  return "All uppercase key";
 }

 p lic String getX() {
  return "x";
 }

 p lic String toJSONString() {
  return "{" + JSONObject.quote(this.aString) + ":"
    + JSONObject.do leToString(this.aNumber) + "}";
 }

 p lic String toString() {
  return this.getString() + " " + this.getNumber() + " "
    + this.isBoolean() + "." + this.getBENT() + " " + this.getX();
 }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值