json字符串多层嵌套解释成java对象——java解释多层嵌套json
Author:zhoulujun@live.cn Date:
我们都知道,"我们都知道,json字符串转java有集中,工具,json-lib,***JsonObjec什么鬼……现在最火的就是GJON,但是,在jsp开发的时候,按照网上扒来的栗子,居然无法跑通"; 然后用fastjson,然后,顺利通过!!
Fastjson API入口类是com.alibaba.fastjson.JSON,常用的序列化操作都可以在JSON类上的静态方法直接完成。
public static final Object parse(String text); // 把JSON文本parse为JSONObject或者JSONArray public static final JSONObject parseObject(String text); // 把JSON文本parse成JSONObject public static final <T> T parseObject(String text, Class<T> clazz); // 把JSON文本parse为JavaBean public static final JSONArray parseArray(String text); // 把JSON文本parse成JSONArray public static final <T> List<T> parseArray(String text, Class<T> clazz); //把JSON文本parse成JavaBean集合 public static final String toJSONString(Object object); // 将JavaBean序列化为JSON文本 public static final String toJSONString(Object object, boolean prettyFormat); // 将JavaBean序列化为带格式的JSON文本 public static final Object toJSON(Object javaObject); 将JavaBean转换为JSONObject或者JSONArray。
参考文章:FastJSON 简介及其Map/JSON/String 互转
在跑的过程中…………fastjson,用源码,居然,提示要spring,不是坑吗?!!!
php代码:
100,
"b"=>array(
array(
"b1"=>"b_value1",
"b2"=>"b_value2",
),
array(
"b1"=>"b_value1",
"b2"=>"b_value2",
)
),
"c"=>array(
"c1"=>"c_value1",
"c2"=>"c_value2"
)
);
java代码:
//import com.google.gson.Gson;
//import com.google.gson.reflect.TypeToken;
//import model.JsonBean;
//import model.User;
import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import model.JsonBean;
import tools.HttpSend;
//import java.lang.reflect.Type;
//import java.util.LinkedList;
/**
* Created by lujun on 3/2/2016.
*/
public class Main {
public static void main(String[] args){
String str;
String str2;
String getParam="{\"openID\":\"xxxxxxxxxxxxxxxx\",\"customerID\":\"28\",\"cityID\":2}";
HttpSend httpSend =new HttpSend();
str=httpSend.sendPost("http://192.168.1.85:8080/jsse/cartManager/getGoodsCount",getParam);
str2=httpSend.sendPost("http://127.0.0.1:8484/site/client/demo.php",getParam);
System.out.print(str);
System.out.print(str2);
String json = "{\"a\":\"100\",\"b\":[{\"b1\":\"b_value1\",\"b2\":\"b_value2\"}, {\"b1\":\"b_value1\",\"b2\":\"b_value2\"}],\"c\": {\"c1\":\"c_value1\",\"c2\":\"c_value2\"}}";
String jsonData = "[{\"username\":\"arthinking\",\"userId\":001},{\"username\":\"Jason\",\"userId\":002}]";
JsonBean jsonBean= JSON.parseObject(json,JsonBean.class);
System.out.print(jsonBean.b.get(0).b1);
// Gson gson = new Gson();
// java.lang.reflect.Type type = new TypeToken() {}.getType();
// JsonBean jsonBean = gson.fromJson(str2, type);
// "我们都知道,json字符串转java有集中,工具,json-lib,***JsonObjec什么鬼……现在最火的就是GJON,但是,在jsp开发的时候,按照网上扒来的栗子,居然无法跑通";
// Type listType = new TypeToken<LinkedList>(){}.getType();
// Gson gson = new Gson();
// LinkedList users = gson.fromJson(jsonData, listType);
}
}
package model;
import java.util.List;
/**
* Created by lujun on 3/2/2016.
*/
public class JsonBean {
public String a;
public List b;
public C c;
}
package model;
/**
* Created by lujun on 3/2/2016.
*/
public class C {
public String c1;
public String c2;
}
package model;
/**
* Created by lujun on 3/2/2016.
*/
public class B {
public String b1;
public String b2;
}
转载本站文章《json字符串多层嵌套解释成java对象——java解释多层嵌套json》,
请注明出处:https://www.zhoulujun.cn/html/java/jsp/2016_0302_7661.html