Android JSON 的封装和解析
package json;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
public class Json {
/**
* @param args
*/
public static void main(String[] args) {
//封装
// JSONObject Json = new JSONObject();
// Json.put(“username”, “root”);
// Json.put(“pwd”, “123456”);
// System.out.print(Json);
// 解析
// String objectStr="{“pwd”:“123456”,“username”:“root”}";
// JSONObject JSB = JSONObject.fromObject(objectStr);
// String pwd = JSB.getString(“pwd”);
// String username = JSB.getString(“username”);
// System.out.print(pwd +username);
//运行结果
{“pwd”:“123456”,“username”:“root”}123456root
// 封装
// JSONObject result = new JSONObject();
// JSONObject json = new JSONObject();
// json.put(“username”, “rrr”);
// result.put(“status”, “1”);
// result.put(“msg”, “register success”);
// result.put(“data”, json);
// System.out.print(result);
// String object =result.toString();
// 解析
// JSONObject jonbject = JSONObject.fromObject(object);
// String msg = jonbject.getString(“msg”);
// String ststus = jonbject.getString(“status”);
// String data = jonbject.getString(“data”);
// System.out.print("\r\n"+msg+"\r\n"+ststus+"\r\n"+data);
// JSONObject jb = JSONObject.fromObject(data);
// String data1 = jb.getString(“username”);
// System.out.print(data1);
// ja.add(json1);
//
// System.out.print("\r\n"+ja);
//
//JSONArray JSA = JSONArray.fromObject(ja);
// JSONObject Objiect = JSA.getJSONObject(0);
//
// JSONObject JSON = JSONObject.fromObject(Objiect);
//
//String s = JSON.getString(“sex”);
//String n = JSON.getString(“name”);
//String a = JSON.getString(“age”);
//
//System.out.print("\r\n"+JSON +"\r\n"+s+"\r\n"+n+"\r\n"+a);
//
// 运行结果
{“msg”:“register success”,“data”:{“username”:“rrr”},“status”:“1”}
register success
1
{“username”:“rrr”}rrr
[{“sex”:“男”,“name”:“xiaoxaio”,“age”:“1”}]
{“sex”:“男”,“name”:“xiaoxaio”,“age”:“1”}
男
xiaoxaio
1
//{
//“sites”: [
//{ “name”:“菜鸟教程” , “url”:“m.runoob.com” },
//{ “name”:“google” , “url”:“www.google.com” },
//{ “name”:“微博” , “url”:“www.weibo.com” }
//]
//}
//封装
JSONObject jso = new JSONObject();
JSONArray jsa = new JSONArray();
JSONObject jso1 = new JSONObject();
JSONObject jso2 = new JSONObject();
JSONObject jso3 = new JSONObject();
jso1.put(“name”, “菜鸟教程”);
jso1.put(“url”, “m.runoob.com”);
jso2.put(“name”, “google”);
jso2.put(“url”, “www.google.com”);
jso3.put(“name”, “微博”);
jso3.put(“url”, “www.weibo.com”);
jsa.add(jso1);
jsa.add(jso2);
jsa.add(jso3);
jso.put(“sites”, jsa);
System.out.print(jso);
//
//
// 解析
JSONObject jsob = JSONObject.fromObject(jso.toString());
String sites= jsob.getString(“sites”);
System.out.print("\r\n"+sites);
JSONArray jsoa = JSONArray.fromObject(sites);
JSONObject name1 = jsoa.getJSONObject(0);
JSONObject name2 = jsoa.getJSONObject(1);
JSONObject name3 = jsoa.getJSONObject(2);
System.out.print("\r\n"+name1);
System.out.print("\r\n"+name2);
System.out.print("\r\n"+name3);
String url1 =name1.getString(“name”);
String url11 =name1.getString(“url”);
String url2 = name2.getString(“name”);
String url22 =name1.getString(“url”);
String url3 = name3.getString(“name”);
String url33 =name1.getString(“url”);
System.out.print("\r\n"+url1);
System.out.print(","+url11);
System.out.print("\r\n"+url2);
System.out.print(","+url22 );
System.out.print("\r\n"+url3);
System.out.print(","+url33 );
//运行结果
{“sites”:[{“name”:“菜鸟教程”,“url”:“m.runoob.com”},{“name”:“google”,“url”:“www.google.com”},{“name”:“微博”,“url”:“www.weibo.com”}]}
[{“name”:“菜鸟教程”,“url”:“m.runoob.com”},{“name”:“google”,“url”:“www.google.com”},{“name”:“微博”,“url”:“www.weibo.com”}]
{“name”:“菜鸟教程”,“url”:“m.runoob.com”}
{“name”:“google”,“url”:“www.google.com”}
{“name”:“微博”,“url”:“www.weibo.com”}
菜鸟教程,m.runoob.com
google,m.runoob.com
微博,m.runoob.com
//
// var sites = [{ “name”:“runoob” , “url”:“m.runoob.com” },
// { “name”:“google” , “url”:“www.google.com” },
// { “name”:“微博” , “url”:“www.weibo.com” }];
//封装
JSONArray JA = new JSONArray();
JSONObject JS1 = new JSONObject();
JSONObject JS2 = new JSONObject();
JSONObject JS3 = new JSONObject();
JS1.put(“name”, “runoob”);
JS1.accumulate(“NAME”, 1);
JS1.put(“url”, “m.runoob.com”);
JS2.put(“name”, “google”);
JS2.put(“url”, “www.google.com”);
JS3.put(“name”, “微博”);
JS3.put(“url”, “www.weibo.com”);
JA.add(JS1);
JA.add(JS2);
JA.add(JS3);
System.out.print(JA);
//
// 解析
JSONArray JSA = JSONArray.fromObject(JA.toString());
JSONObject js = JSA.getJSONObject(0);
JSONObject js1 = JSA.getJSONObject(1);
JSONObject js2 = JSA.getJSONObject(2);
System.out.print("\r\n"+js+"\r\n"+js1+"\r\n"+js2);
String name1 = js.getString("name");
String url1 = js.getString("url");
int NAME = js.getInt("NAME");
String name2 = js1.getString("name");
String url2 = js1.getString("url");
String name3 = js2.getString("name");
String url3 = js2.getString("url");
System.out.print("\r\n"+name1+","+url1+","+"NAME"+"\r\n"+name2+","+url2+"\r\n"+name3+","+url3);
//运行结果
[{“name”:“runoob”,“url”:“m.runoob.com”,“NAME”:1},{“name”:“google”,“url”:“www.google.com”},{“name”:“微博”,“url”:“www.weibo.com”}]
{“name”:“runoob”,“url”:“m.runoob.com”,“NAME”:1}
{“name”:“google”,“url”:“www.google.com”}
{“name”:“微博”,“url”:“www.weibo.com”}
runoob,m.runoob.com,NAME
google,www.google.com
微博,www.weibo.com
}
}