java代码(处理json串)
Posted 新美好时代
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java代码(处理json串)相关的知识,希望对你有一定的参考价值。
package test;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
public class DealJsonString {
/* 把JSON文本parse为JSONObject或者JSONArray
public static final Object parse(String text)
把JSON文本parse成JSONObject
public static final JSONObject parseObject(String text)
把JSON文本parse成JSONArray
public static final JSONArray parseArray(String text)
将JavaBean转换为JSONObject或者JSONArray
public static final Object toJSON(Object javaObject)
把JSON文本parse为JavaBean
public static final T parseObject(String text, Class clazz)
把JSON文本parse成JavaBean集合
public static final List parseArray(String text, Class clazz)
将JavaBean序列化为JSON文本
public static final String toJSONString(Object object);
将JavaBean序列化为带格式的JSON文本
public static final String toJSONString(Object object, boolean prettyFormat)
*/
public static vopersonId main(String[] args) {
String jsonStr1 = "{\"person\":{\"personId\":100127,\"phone\":\"13811821600\"}}";
String jsonStr2 = "{\"code\": 0,\"data\": [{\"personType\": 2,\"housepersonId\": 201010091001,\"type\": 3,\"personId\": 100684,},{\"personType\": 1,\"housepersonId\": 201010091002,\"type\": 2,\"personId\": 201010091003,}],\"dataExtra\": null,\"msg\": \"OK\"}";
String jsonStr3 = "{\"data\":{\"pictureList\":[{\"bigPictureFilePath\":\"http://ip:port//vpersonIdeoFile/201001021001.jpg\",\"mpersonIddlePictureFilePath\":\"http://ip:port//vpersonIdeoFile/201001021002.jpg\",\"smallPictureFilePath\":\"http://ip:port//vpersonIdeoFile/201001021003.jpg\"}],\"vpersonIdeoFilePaht\":\"http://ip:port//vpersonIdeoFile/201001021001.mp4\"},\"code\":0,\"msg\":\"成功\"}";
String jsonStr4 = "{\"listResult\": {\"curPage\": 1,\"resultCount\": 2,\"totalCount\": 32,\"pageSize\": 10,\"start\": 0,\"end\": 10,\"orderName\": \"personId\",\"sortedBy\": \"desc\",\"data\": null,\"page\": true},\"data\": [{\"personId\": 201001021001,\"status\": 2,\"content\": \"内容2\",\"category\": 3},{\"personId\": 201001021002,\"status\": 2,\"content\": null,\"category\": 3},{\"personId\": 201001021003,\"status\": 1,\"content\": null,\"category\": 2},{\"personId\": 201001021004,\"status\": 2,\"content\": \"内容3\",\"category\": 3}],\"status\": 0,\"msg\": null}";
String jsonStr5 = "{\"listResult\": {\"curPage\": 1},\"data\": {\"houses\": [{\"personId\": 201001021001,\"status\": 2,},{\"personId\": 201001021101,\"status\": 3},{\"personId\": 201001021031,\"status\": 3},{\"personId\": 201001021303,\"status\": 3}],\"overall\": {\"payMoney\": 0,\"houseNum\": 2}},\"status\": 0,\"msg\": null}";
String jsonStr6 = "{\"listResult\":{\"curPage\":1,\"resultCount\":0,\"totalCount\":0,\"pageSize\":20,\"start\":0,\"end\":20,\"orderName\":\"personId\",\"sortedBy\":\"desc\",\"data\":null,\"page\":true},\"data\":{\"houses\":[],\"overall\":{\"payMoney\":0.0,\"houseNum\":0}},\"status\":0,\"msg\":null}";
String jsonStr7 = "{\"code\":0,\"msg\":\"0\",\"data\":{\"paypayMoney\":null}}";
//如果是对象用 getJSONObject("data"); 如果是array 用getJSONArray("data") 单独的字段用 getString("")获取;
// 将相应结果转换为json对象
JSONObject jsonResponse = JSON.parseObject(jsonStr7);
// 获取data json对象
JSONObject jsonData = jsonResponse.getJSONObject("data");
float paypayMoney=0.0f;
try {
String tempPaypayMoney = jsonData.getString("paypayMoney");
paypayMoney = Float.parseFloat(tempPaypayMoney);
} catch (Exception e) {
paypayMoney=0.0f;
}
System.out.println(String.valueOf(paypayMoney));
}
}
以上是关于java代码(处理json串)的主要内容,如果未能解决你的问题,请参考以下文章