json lib java_json-lib 解析实例

1.数组转换为json字符串(用JSONArray)String array[] = new String[]{"one","two","three"};

JSONArray jsonArray= JSONArray.fromObject(array);

System.out.println(jsonArray);

结果:["one","two","three"]

2.list转换为json字符串(用JSONArray)List list = new ArrayList();

list.add("1");

list.add("2");

list.add("3");

JSONArray jsonArray = JSONArray.fromObject(list);

System.out.println(jsonArray);

结果:["1","2","3"]

3.Map转换为json字符串(用JSONObject)Map map = new HashMap();

map.put("name", "junxie");

map.put("age", "23");

map.put("county", "china");

JSONObject jsonObject = JSONObject.fromObject(map);

System.out.println(jsonObject);

结果:{"county":"china","age":"23","name":"junxie"}

4.普通java对象转为json字符串

user对象:public class User {

public String name;

public String age;

public String country;

public User(){}

public User(String name,String age,String country){

this.name = name;

this.age = age;

this.country = country;

}

//省略get和set方法

}User user = new User("junxie","23","china");

JSONObject jsonObject = JSONObject.fromObject(user);

System.out.println(jsonObject);

结果:{"age":"23","country":"china","name":"junxie"}

注意:和map的结果一样的

5.复杂的java对象转为json字符串(对象中包含list和map)

UserScores对象:public class UserScores{

private List scores;   //list

private String name;

private String age;

private Map scores2;    //map

.....//省略get和set方法

}List scores = new ArrayList();

scores.add("100");

scores.add("98");    //组装list

Map scores2 = new HashMap();

scores2.put("chinese", "80");

scores2.put("math", "90");    //组装map

UserScores useScores = new UserScores();

useScores.setAge("23");

useScores.setName("junxie");

useScores.setScores(scores);

useScores.setScores2(scores2);

JSONObject jsonObject = JSONObject.fromObject(useScores);

System.out.println(jsonObject);

结果:

{"age":"23","name":"junxie","scores":["100","98"],"scores2":{"math":"90","chinese":"80"}}

注意:scores为list的生成结果,scores2为map的生成结果

6.json字符串转为普通对象String jsonString = "{age:'23',country:'china',name:'junxie'}";

JSONObject jsonObject = JSONObject.fromObject(jsonString);

User user = (User)JSONObject.toBean(jsonObject,User.class);

System.out.println(user.getName());

结果:打印的name为 junxie

注意:先生成jsonObject,再调用静态方法JSONObject.toBean(jsonObject, beanClass)

7.json字符串转为复杂对象(对象中含有list,map或者其他对象)String jsonList = "{age:'23',name:'junxie',scores:['100','98'],scores2:{math:'90',chinese:'80'}}";  //待解析的json字符串

Map map = new HashMap();

map.put("scores", String.class);  //复杂对象list

map.put("scores2", Map.class);    //复杂对象map  (可以有其他的子对象等)

JSONObject jsonObject = JSONObject.fromObject(jsonList);

UserScores userScores= (UserScores)JSONObject.toBean(jsonObject, UserScores.class, map);     //注意该方法中要放入之前建立的map

System.out.println(userScores);

List scores = userScores.getScores();

System.out.println(scores);

Map scores2 = userScores.getScores2();

System.out.println(scores2);

结果:

UserScores@739495b8

[100, 98]

{math=90, chinese=80}

注意:如果要解析成复杂对象时,应该先建立Map,放入所有的复杂对象类型,最后调用方法

JSONObject.toBean(jsonObject, beanClass, classMap)  该classMap则放入所有复杂对象的map

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值