JSON —— org.json包使用测试

package org.json;

import junit.framework.TestCase;

public class Test extends TestCase {

	/*{
	"JSONArray": [],
	"JSONObject": {},
	"String": "98.6",
	"\\u2028": "\u2028",
	"\\u2029": "\u2029",
	"bool": "true",
	"double": 1.2345678901234568E29,
	"false": false,
	"foo": [
	    true,
	    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>.",
	    true,
	    false,
	    [],
	    {}
	],
	"int": 57,
	"keys": [
	    "foo",
	    "op",
	    "String",
	    "JSONObject",
	    "\\u2028",
	    "ten",
	    "true",
	    "int",
	    "bool",
	    "\\u2029",
	    "to",
	    "double",
	    "JSONArray",
	    "null",
	    "zero",
	    "false"
	],
	"null": null,
	"op": "Good",
	"ten": 10,
	"to": null,
	"true": true,
	"zero": -0
	}
	String: 98.6
	bool: true
	to: null
	true: true
	foo: [true,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>.",true,false,[],{}]
	op: Good
	ten: 10
	oops: false
	*/
	public void testReadJSON() {
		JSONArray jsonArray;
		JSONObject jsonObj;
		try {
			jsonObj = new JSONObject(
					"{foo: [true, 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("double", 123456789012345678901234567890.);
			jsonObj.put("true", true);
			jsonObj.put("false", false);
			jsonObj.put("null", JSONObject.NULL);
			jsonObj.put("bool", "true");
			jsonObj.put("zero", -0.0);
			jsonObj.put("\\u2028", "\u2028");
			jsonObj.put("\\u2029", "\u2029");
			jsonArray = jsonObj.getJSONArray("foo");
			jsonArray.put(666);
			jsonArray.put(2001.99);
			jsonArray.put("so \"fine\".");
			jsonArray.put("so <fine>.");
			jsonArray.put(true);
			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.getDouble("String"));
			System.out.println("  bool: " + jsonObj.getBoolean("bool"));
			System.out.println("    to: " + jsonObj.getString("to"));
			System.out.println("  true: " + jsonObj.getString("true"));
			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": true,
	"aNumber": 42,
	"aString": "A beany object"
	}
	*/
	public void testBean() {
		Obj obj = new Obj("A beany object", 42, true);
		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]
	 */
	public 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"]]]}
	 */
	public void testJsonStringer() {
		JSONStringer jsonStringer;
		String str;
		jsonStringer = new JSONStringer();
		try {
			str = jsonStringer.object().key("single").value("MARIE HAA'S").key(
					"Johnny").value("MARIE HAA\\'S").key("foo").value("bar")
					.key("baz").array().object().key("quux").value(
							"Thanks, Josh!").endObject().endArray().key(
							"obj keys").value("11").endObject().toString();

			System.out.println(str);

			System.out.println(new JSONStringer().object().key("a").array()
					.array().array().value("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"
	    }
	],
	"instructions": {"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":"instructions"}],"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><instructions><step>Knead thoroughly.</step><step>Knead again.</step></instructions></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"
	],
	[
	    "instructions",
	    [
	        "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><instructions><step>Knead thoroughly.</step><step>Knead again.</step></instructions></recipe>
	*/
	public 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> "
				+ " <instructions> "
				+ " <step>Knead thoroughly.</step>"
				+ " <step>Knead again.</step> "
				+ " </instructions> "
				+ "</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>
	*/
	public 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 {
	public String aString;
	public double aNumber;
	public boolean aBoolean;

	public Obj(String string, double n, boolean b) {
		this.aString = string;
		this.aNumber = n;
		this.aBoolean = b;
	}

	public double getNumber() {
		return this.aNumber;
	}

	public String getString() {
		return this.aString;
	}

	public boolean isBoolean() {
		return this.aBoolean;
	}

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

	public String getX() {
		return "x";
	}

	public String toJSONString() {
		return "{" + JSONObject.quote(this.aString) + ":"
				+ JSONObject.doubleToString(this.aNumber) + "}";
	}

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

 

你可能感兴趣的:(xml,json,JUnit)