net.sf..json.JSONObjec和com.alibaba.fastjson.JSONObject实践

JSONObject jsonObject = new JSONObject();

引入的是com.alibaba.fastjson.JSONObject类,用Put可以加值,是比较快的json转化类
jsonObject.put("messageId", messageId);


测试类代码如下
package jsonTest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class JosnTest {
//对象转化,list转化,map 转化,List<Map<string,Object>>的一个转化
public static void main(String[] args) {
JSONObject jsonobject= new JSONObject();
jsonobject.put("name", "小妹小妹");
jsonobject.put("age", "3");
System.out.println(jsonobject.toJSONString());
Person person=new Person();
person.setAge("10");
person.setName("xx");
JSONObject jsonBean=(JSONObject) JSONObject.toJSON(person);
System.out.println(jsonBean.toString());
jsonobject.put("小明", jsonBean);
System.out.println(jsonobject);
JSONObject jsonBean3=jsonobject.getJSONObject("小明");
System.out.println(jsonBean3);
Person parseBean=JSONObject.toJavaObject(jsonBean3, Person.class);
Person parseBean2=JSONObject.parseObject(jsonBean3.toJSONString(), Person.class);
System.out.println("parseBean="+parseBean.getAge());
System.out.println("parseBean2="+parseBean.getAge());
String strname =jsonBean3.getString("name");
System.out.println(strname);
JSONArray jsonArray=new JSONArray();
jsonArray.add(jsonBean3);
jsonArray.add(person);
System.out.println(jsonArray.toString());
List<Person> personList=JSONArray.parseArray(jsonArray.toString(), Person.class);
String str=(String) JSONArray.toJSONString(personList);
System.out.println(str);
List<Person> js=JSONArray.parseArray(str,Person.class);
System.out.println(personList.get(0).getAge());
System.out.println("js="+personList.get(0).getAge());
Map<String,String> mp =JSONObject.parseObject(jsonBean3.toJSONString(), Map.class);
System.out.println(mp.toString());
List<Map<String,String>> listmap =JSONObject.parseObject(jsonArray.toString(), List.class);
System.out.println(listmap.get(0).get("name"));
}

}







class Person{
private String name;
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}




1,net.sf.json.JSONObject
JSONObject是一个无序的键值对集合。值可以是任意类型:Boolean,JSONArray,JSONObject,Number,String,or the JSONObject.NULL Object
2,net.sf.json.JSONArray
JSONArray是一个有序的值序列。值可以是任意类型:Boolean, JSONArray, JSONObject, Number, String, or the JSONNull object。
形如:[  "locality", "political" 


//对象转化,list转化,map 转化,List<Map<string,Object>>的一个转化
package jsonTest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class NetJsonTest {
public static void main(String[] args) {

JSONObject jsonObject =new JSONObject();
jsonObject.put("name", "小妹小妹");
jsonObject.put("age", "3");
System.out.println(jsonObject.toString());
Person tempPerson= (Person) JSONObject.toBean(jsonObject, Person.class);
// Person tempPerson= (Person) JSONObject.toBean(jsonObject, beanClass);
System.out.println("tempPerson.name="+tempPerson.getName());
JSONObject returnObject=JSONObject.fromObject(tempPerson);
Person tempPerson2=new Person();
System.out.println(returnObject.toString());
tempPerson2.setAge("10");
tempPerson2.setName("小花小花");
List personList=new ArrayList<Person>();
personList.add(tempPerson2);
personList.add(tempPerson);
JSONArray jsonArray=JSONArray.fromObject(personList);
System.out.println("list变为string="+jsonArray.toString());
//变为list
List<Person> personList2=JSONArray.toList(jsonArray, Person.class);
System.out.println(personList2.get(0).getAge());
HashMap<String, String> map=new HashMap<String,String>();
if(!jsonObject.isNullObject()){
Iterator<String> it=jsonObject.keys();
while(it.hasNext()){
String key=it.next();
map.put(key, (String) jsonObject.get(key));
}
}
System.out.println(map.toString());
JSONObject mapJson=JSONObject.fromObject(map);
System.out.println("mapJson="+mapJson.toString());
}

}

以上是对net.sf..json.JSONObjec和com.alibaba.fastjson.JSONObject的一个转化。


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值