(JAVA )json解析数据有空值解析不了怎么办?
"itemMajorInfo":{"iD":333,"craftName":"手工_多位砂","colorFlag":0,"startQuantity":1000,"em...
"itemMajorInfo": {
"iD": 333,
"craftName": "手工_多位砂",
"colorFlag": 0,
"startQuantity": 1000,
"employeeName": "",
"startTime": ""
} 展开
"iD": 333,
"craftName": "手工_多位砂",
"colorFlag": 0,
"startQuantity": 1000,
"employeeName": "",
"startTime": ""
} 展开
2个回答
展开全部
json里面有空值是可以解析的 就是解析出来的值为空
JSONObject ob = JSONObject.fromObject(json);
JSONObject itemMajorInfo = ob.JSONObject("itemMajorInfo");
String employeeName = itemMajorInfo.getString("employeeName");
System.out.println(employeeName);//值为空
JSONObject ob = JSONObject.fromObject(json);
JSONObject itemMajorInfo = ob.JSONObject("itemMajorInfo");
String employeeName = itemMajorInfo.getString("employeeName");
System.out.println(employeeName);//值为空
更多追问追答
追问
我就是这么写的,这样写了之后前面的数据都解不出来,去掉这个就能解析前面和后面的数据
追答
你把程序发一下!我在我这试试
import net.sf.json.JSONObject;
public class test {
public static void main(String[] args) {
String json = "{'itemMajorInfo': {'iD': 333,'craftName': '手工_多位砂','colorFlag': 0, 'startQuantity': 1000,'employeeName': '','startTime': ''}}";
JSONObject ob = JSONObject.fromObject(json);
JSONObject itemMajorInfo = ob.getJSONObject("itemMajorInfo");
String iD = itemMajorInfo.getString("iD");
String employeeName = itemMajorInfo.getString("employeeName");
System.out.println("iD:"+iD);//(iD:333)
System.out.println("employeeName:"+employeeName);//值为空(employeeName:)
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询