import Net.sf.json.JSONArray;
import net.sf.json.JSONException;
Import Net.sf.json.JSONObject;
Import Net.sf.json.JSONSerializer;
/**
* JSON objects are converted to Java objects
*
* @throws jsonexception
*/
@Test
public void Jsontojava () {
String json= "[{\" addtime\ ": \" 2011-09-19 14:23:02\ ", \" iccid\ ": \" 1111\ ", \" id\ ": 0,\" imei\ ": \" 2222\ ", \" imsi\ ": \" 3333 \ ", \" phonetype\ ": \" 4444\ ", \" Remark\ ": \" aaaa\ ", \" tel\ ": \" 5555\ "}]";
Receives the {} object, where the array object is received with an exception
if (Json.indexof ("[")!=-1) {
Json=json.replace ("[", "" ");
}
if (Json.indexof ("]")!=-1) {
Json=json.replace ("]", "" ");
}
Jsonobject obj=new Jsonobject (). Fromobject (JSON);
Siminfo siminfo= (siminfo) Jsonobject.tobean (obj, siminfo.class);
System.out.println ("obj:" +siminfo);
System.out.println (Siminfo.getaddtime ());
System.out.println (Siminfo.geticcid ());
System.out.println (Siminfo.getimei ());
System.out.println (Siminfo.getimsi ());
System.out.println (Siminfo.getphonetype ());
System.out.println (Siminfo.getremark ());
System.out.println (Siminfo.gettel ());
System.out.println (Siminfo.getid ());
Daofactory.getsiminfodao (). Add (Siminfo);
}
/**
* Convert JSON to Java collection Object
*/
@Test
public void Jsontojavas () {
String jsons= "[{\" addtime\ ": \" 2011-09-19 14:23:02\ ", \" iccid\ ": \" 1111\ ", \" id\ ": 0,\" imei\ ": \" 2222\ ", \" imsi\ ": \" 3333\ ", \" phonetype\ ": \" 4444\ ", \" Remark\ ": \" aaaa\ ", \" tel\ ": \" 5555\ "}," +
"{\" addtime\ ": \" 2011-11-11 14:23:02\ ", \" iccid\ ": \" 2222\ ", \" id\ ": 0,\" imei\ ": \" 2222\ ", \" imsi\ ": \" 3333\ ", \" Phonetype\ ": \" 4444\ ", \" Remark\ ": \" aaaa\ ", \" tel\ ": \" 5555\ "}]";
list<siminfo> Siminfos = getjavacollection (New Siminfo (), jsons);
System.out.println (Siminfos.size ());
for (Siminfo Siminfo:siminfos) {
System.out.println ("Addtime:" +siminfo.getaddtime ());
System.out.println ("=========");
}
}
/**
* Encapsulation converts a JSON object to a Java collection object
*
* @param <T>
* @param clazz
* @param jsons
* @return
*/
Private <T> list<t> getjavacollection (T clazz, String jsons) {
List<t> Objs=null;
Jsonarray jsonarray= (Jsonarray) Jsonserializer.tojson (jsons);
if (jsonarray!=null) {
Objs=new arraylist<t> ();
List list= (list) Jsonserializer.tojava (Jsonarray);
for (Object o:list) {
Jsonobject Jsonobject=jsonobject.fromobject (o);
T obj= (t) Jsonobject.tobean (Jsonobject, Clazz.getclass ());
Objs.add (obj);
}
}
return OBJS;
}
/**
* Java objects are converted to JSON objects
*
* @throws jsonexception
*/
@Test
public void Javatojson () {
Siminfo siminfo=new siminfo ();
Siminfo.setaddtime (Utiltool.datetostr (New Date (), null));
Siminfo.seticcid ("1111");
Siminfo.setimei ("2222");
Siminfo.setimsi ("3333");
Siminfo.setphonetype (4);
Siminfo.setremark ("AAAA");
Siminfo.settel ("5555");
Converting Java objects to JSON objects
String json=new Jsonarray (). Fromobject (Siminfo). toString ();
JSON: [{"Addtime": "2011-09-19 14:23:02", "Iccid": "1111", "id": 0, "IMEI": "2222", "IMSI": "3333", "Phonetype": "4444", " Remark ":" AAAA "," tel ":" 5555 "}]
System.out.println ("JSON:" +json);
}
Jackson-core-asl-1.7.2.jar
Jackson-mapper-asl-1.7.2.jar
Convert a Java object to JSON
StringWriter writer = new StringWriter ();
Objectmapper mapper = new Objectmapper ();
Mapper.writevalue (writer, siminfo);
String json=writer.tostring ();
Based on the Json-lib.jar package JSON instance program
1.JSONObject to Dynabean
String json = "{name=\" json\ ", bool:true,int:1,double:2.2}";
Jsonobject Jsonobject = Jsonobject.fromobject (JSON);
Abstract notation: Dynabean bean = (dynabean) Jsonserializer.tojava (jsonobject);
Object bean = Jsonobject.tobean (jsonobject);
Object bean1 = Jsonserializer.tojava (Jsonobject);
Assertequals (Jsonobject.get ("name"), Propertyutils.getproperty (Bean, "name"));
Assertequals (Jsonobject.get ("bool"), Propertyutils.getproperty (Bean, "bool"));
Assertequals (Jsonobject.get ("int"), Propertyutils.getproperty (Bean, "int"));
Assertequals (Jsonobject.get ("Double"), Propertyutils.getproperty (Bean, "double"));
2.JSONObject to JavaBean
String json = "{name:\" zhangsan\ ", age:25,hight:1.72,sex:true} ";
Jsonobject jsonobject = Jsonobject.fromobject (JSON);
UserBean bean = (UserBean) Jsonobject.tobean (Jsonobject, Userbean.class);
System.out.println (Jsonobject);
Theoretically, this is possible, but when there is an exception caused by:java.lang.nosuchmethodexception:com.json.json$userbean.<init> ()
3.JSONArray to List
String json = "[\" first\ ", \" second\ "]";
Jsonarray Jsonarray = (jsonarray) Jsonserializer.tojson (JSON);
List output = (list) Jsonserializer.tojava (Jsonarray);
4.JSONArray to array
String json = "[\" first\ ", \" second\ "]";
Jsonarray Jsonarray = (jsonarray) Jsonserializer.tojson (JSON);
Jsonconfig jsonconfig = new Jsonconfig ();
Jsonconfig.setarraymode (Jsonconfig.mode_object_array);
object[] output = (object[]) Jsonserializer.tojava (Jsonarray, jsonconfig);
Object[] expected = new object[] {"First", "second"};
Arrayassertions.assertequals (expected, output);
5.JSON string specifically for JavaBean (Liu Huibin Demo demo required jar package in the attachment)
String str= "[{\" id\ ": \" 328\ ", \" mestype\ ": \" inbox\ "},{\" id\ ": \" 327\ ", \" mestype\ ": \" inbox\ "},{\" id\ ": \" 279\ ", \" Mestype\ ": \" already\ "},{\" id\ ": \" 278\ ", \" mestype\ ": \" already\ "},{\" id\ ": \" 277\ ", \" mestype\ ": \" already\ "},{\" id \ ": \" 310\ ", \" mestype\ ": \" inbox\ "},{\" id\ ": \" 308\ ", \" mestype\ ": \" inbox\ "},{\" id\ ": \" 305\ ", \" mestype\ ": \" Inbox\ " },{\ "id\": \ "304\", \ "mestype\": \ "inbox\"},{\ "id\": \ "303\", \ "mestype\": \ "inbox\"}] ";
Jsonarray jsonarray= (jsonarray) Jsonserializer.tojson (str);
List list= (list) Jsonserializer.tojava (Jsonarray);
For (Object obj:list) {
Jsonobject jsonobject = jsonobject.fromobject (obj);
Messagebean bean = (messagebean) Jsonobject.tobean (Jsonobject, Messagebean.class);
String Id=bean.getid () + "";
String Type=bean.getmestype ();
System.out.println (id+ "" +type);
}
System.out.println (List.size ());
Conversion of JSON to Java data