fastjson与各类型的转换参考:
/**字符串转Json数组并解析**/
try {
JSONArray array = JSONArray.parseArray(line));
String[] fields = {"lat", "lng", "poi_id", "poi_name", "poi_type", "roadidx"};
if (null != array) {
Iterator<Object> iter = array.iterator();
while (iter.hasNext()) {
JSONObject json = (();
for (int i = 0; i < fields.length; i++) {
System.out.String(fields[i]));
}
}
}
}catch (JSONException e) {
println("json paese exception = " + jsonStr);string字符串转化数组
}
/**对JSONObject中的指定字段重新赋值**/
JSONObject posJson = new JSONObject();
posJson.put("op", "in");
/**其它**/
public class JsonDemo {
public static void main(String[] args) {
//1.json字符串转换为对象
String jsonString="{'name':'42313123','id':'2345','age':12}";
JSONObject jsonObject = JSONObject.parseObject(jsonString);
String id = String("id");
System.out.println(id);
//2. JSONObject转化成⾃定义类对象
PeoplePo peoplePo1 = JSONObject.parseObject(jsonString, PeoplePo.class);
System.out.println(peoplePo1);
//3. JSONObject转化成Map集合
Map map = JSONObject.parseObject(jsonString, Map.class);
System.out.println(map);
//4. ⾃定义对象转化成json格式的字符串
PeoplePo peoplePo = new PeoplePo();
peoplePo.setId("1");
peoplePo.setAge(11);
peoplePo.setName("LH");
String peopleJson = JSONString(peoplePo);
System.out.println(peopleJson);
//5. String类型转化成JSONObject;
String str = "{\"result\":\"success\",\"message\":\"成功!\"}";
JSONObject jsonObject1 = JSONObject.parseObject(str);
System.out.println(jsonObject1);
//6. JSONObject转化成JSONArray的两种⽅式
String str1 = "{\"result\":\"success\",\"message\":\"成功!\",\"data\":[{\"name\":\"Tom\",\"age\":\"20\"}]}";
JSONObject jsonToArray = JSONObject.parseObject(str1);
//⽅式⼀
JSONArray data = JSONArray("data");
System.out.println(data);
//⽅式⼆
JSONArray jsonArray = JSONArray.String("data"));
System.out.println(jsonArray);
//7. jsonArray转化成JSONObject并取出其中的元素数据 JSONObject o = (JSONObject) (0);
String name = o.getString("name");
System.out.println(o);
System.out.println(name);
System.out.String());
}
}
//转⾃ blog.csdn/lh19960914/article/details/87861308
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论