怎么将这段复杂的JSON字符串转换成实体类,每次转都报错?
复杂的字符串
String jsonStr = "{'PACKAGE':{'DATA':{'RECORD':{'MSG':'接口调用成功','NO':'1','CODE':'10','CONDITION':{'NO':'1','exp':''},'RESULT':[{'NUM':'1','ROW':[{'ZZBF_RQ':'2022-08-25 17:08:01','DZZZMC':'户籍事项证明','GMSFHM':'110102198010272321','DZZZBZ':'1.2.156.3005.2.11100000000013127D082.110002002000.0822022082500000002.001.L','DZZZLXDM':'11100000000013127D082','DZZZYXX_PDBZ':'1','ZMRQ':'2022-08-25','ZZBFJG_DWMC':'北京市西城分局西长安街派出所','INDEX':'1','YYFW_YWLSH':'54347563-AD16-48EB-B3AC-E9FB631988A9','ZAGLDZZZBH':'0822022082500000002'},{'NUM':1,'ROW':[{'DZWJNR':'UEAAgACAAAiRAAA','INDEX':1}],'SID':'05070400'}],'SID':'05010402'}]}},'PACKAGEHEAD':{'MSG':'接口调用成功','SYDW_GAJGJGDM':'010300000000','CODE':'10','SYDW_GAJGMC':'北京市公安局西城分局','FSSJ':'20110112110221','SYR_XM':'李四','SYR_GMSFHM':'110102198010272324','BBH':'3.00'}}}";
完整的代码,
package com.ssoft.util;import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import java.util.ArrayList;
import java.util.List;
/**
*
* @date 2022/2/16
*/
@Data
@Slf4j
public class ResultDownloadEntity implements ResultInterface {
@JSONField(name = "PACKAGE")
PACKAGE PACKAGE=new PACKAGE();
@Data
public static class PACKAGE {
@JSONField(name = "PACKAGEHEAD")
PACKAGEHEAD PACKAGEHEAD = new PACKAGEHEAD();
@JSONField(name = "DATA")
ResultEntity.PACKAGE.DATA DATA=new ResultEntity.PACKAGE.DATA();
@Data
public static class PACKAGEHEAD {
@JSONField(name = "BBH")
String BBH;
@JSONField(name = "SYDW_GAJGJGDM")
String SYDW_GAJGJGDM;
@JSONField(name = "SYDW_GAJGMC")
String SYDW_GAJGMC;
@JSONField(name = "SYR_XM")
String SYR_XM;
@JSONField(name = "SYR_GMSFHM")
String SYR_GMSFHM;
@JSONField(name = "FSSJ")
String FSSJ;
@JSONField(name = "SJBBH")
String SJBBH;
@JSONField(name = "CODE")
String CODE = "10";
@JSONField(name = "MSG")
String MSG = "接口调用成功";
}
@Data
public static class DATA {
@JSONField(name = "RECORD")
RECORD RECORD = new RECORD();
@Data
public static class RECORD {
@JSONField(name = "CODE")
String CODE = "10";
@JSONField(name = "NO")
String NO="1";
@JSONField(name = "MSG")
String MSG = "接口调用成功";
@JSONField(name = "CONDITION")
CONDITION CONDITION = new CONDITION();
@JSONField(name = "RESULT")
List<Object> RESULT = new ArrayList<>();
@Data
public static class CONDITION {
@JSONField(name = "NO")
String NO = "1";
@JSONField(name = "exp")
String exp="";
}
}
}
}
public static JSONObject decodeRow(String jsonStr) {
JSONObject recordJSON = new JSONObject();
try {
JSONObject jsonObject = JSON.parseObject(jsonStr);
JSONObject packageJSON = jsonObject.getJSONObject("PACKAGE");
JSONObject dataJSON = packageJSON.getJSONObject("DATA");
recordJSON = dataJSON.getJSONObject("RECORD");
JSONArray resultArray = recordJSON.getJSONArray("RESULT");
JSONObject rowQuery = (JSONObject) resultArray.get(0);
// log.info(rowQuery.toJSONString());
// log.info(dataJSON.toJSONString());
} catch (Exception e) {
e.printStackTrace();
}
return recordJSON;
}
public static void main(String[] args) {
JSONObject jsonObject = new JSONObject();
String jsonStr = "{'PACKAGE':{'DATA':{'RECORD':{'MSG':'接口调用成功','NO':'1','CODE':'10','CONDITION':{'NO':'1','exp':''},'RESULT':[{'NUM':'1','ROW':[{'ZZBF_RQ':'2022-08-25 17:08:01','DZZZMC':'户籍事项证明','GMSFHM':'110102198010272321','DZZZBZ':'1.2.156.3005.2.11100000000013127D082.110002002000.0822022082500000002.001.L','DZZZLXDM':'11100000000013127D082','DZZZYXX_PDBZ':'1','ZMRQ':'2022-08-25','ZZBFJG_DWMC':'北京市西城分局西长安街派出所','INDEX':'1','YYFW_YWLSH':'54347563-AD16-48EB-B3AC-E9FB631988A9','ZAGLDZZZBH':'0822022082500000002'},{'NUM':1,'ROW':[{'DZWJNR':'UEAAgACAAAiRAAA','INDEX':1}],'SID':'05070400'}],'SID':'05010402'}]}},'PACKAGEHEAD':{'MSG':'接口调用成功','SYDW_GAJGJGDM':'010300000000','CODE':'10','SYDW_GAJGMC':'北京市公安局西城分局','FSSJ':'20110112110221','SYR_XM':'李四','SYR_GMSFHM':'110102198010272324','BBH':'3.00'}}}";
try {
ResultDownloadEntity bean = JSONObject.parseObject(jsonStr, ResultDownloadEntity.class);
log.info("ROW == " + bean.getPACKAGE().getDATA().getRECORD().getRESULT());
} catch (Exception e) {
e.printStackTrace();
}
// decodeRow(jsonStr);
}
}
回答:
你这个就不是json字符串吧. json只能使用双引号, 单引号是非法的.
回答:
public static void main(String[] args) { String jsonStr = "{'PACKAGE':{'DATA':{'RECORD':{'MSG':'接口调用成功','NO':'1','CODE':'10','CONDITION':{'NO':'1','exp':''},'RESULT':[{'NUM':'1','ROW':[{'ZZBF_RQ':'2022-08-25 17:08:01','DZZZMC':'户籍事项证明','GMSFHM':'110102198010272321','DZZZBZ':'1.2.156.3005.2.11100000000013127D082.110002002000.0822022082500000002.001.L','DZZZLXDM':'11100000000013127D082','DZZZYXX_PDBZ':'1','ZMRQ':'2022-08-25','ZZBFJG_DWMC':'北京市***出所','INDEX':'1','YYFW_YWLSH':'54347563-AD16-48EB-B3AC-E9FB631988A9','ZAGLDZZZBH':'0822022082500000002'},{'NUM':1,'ROW':[{'DZWJNR':'UEAAgACAAAiRAAA','INDEX':1}],'SID':'05070400'}],'SID':'05010402'}]}},'PACKAGEHEAD':{'MSG':'接口调用成功','SYDW_GAJGJGDM':'010300000000','CODE':'10','SYDW_GAJGMC':'北京市公安局西城分局','FSSJ':'20110112110221','SYR_XM':'李四','SYR_GMSFHM':'1101***72324','BBH':'3.00'}}}";
final Map<String,Map> map = JSON.parseObject(jsonStr, Map.class);
final Object data = map.get("PACKAGE").get("DATA");
Map<String,Map> recordMap = null;
if (data instanceof Map){
recordMap = (Map<String, Map>) data;
}
if (MapUtils.isEmpty(recordMap)){
return;
}
final Object result = recordMap.get("RECORD").get("RESULT");
System.out.println("JSON.toJSONString(result) = " + JSON.toJSONString(result));
}
主要我没跟你弄那个实体,自己再解析下吧,还要不要随便泄露真实数据。
回答:
- 报错信息是什么?
JsonProperty
是 jacskon 的,JSONObject
是 fastjson 的,要么用 jacskon 解析,要么换 fastjson 的注解
以上是 怎么将这段复杂的JSON字符串转换成实体类,每次转都报错? 的全部内容, 来源链接: utcz.com/p/944669.html