[英]How to convert Empty string to null in JsonArray ( com.google.gson)
My JSON Object has some empty values ("") So While converting it throws error. 我的JSON对象有一些空值(“”),因此在转换时会引发错误。
Here's my JSON 这是我的JSON
{
"Animals": [
{
"id": 6140,
"Name": ""
},
{
"id": 6144,
"Name": "Lion"
}
]
}
My Code: 我的代码:
JsonParser parser=new JsonParser();
JsonArray myarray=parser.parse(json).getAsJsonArray();
I'm getting the error as 我收到错误信息
com.google.gson.stream.malformedjsonexception
com.google.gson.stream.malformedjsonexception
How do I convert the empty string values into null while parsing the JSON object as a JSONArray? 在将JSON对象解析为JSONArray时,如何将空字符串值转换为null?
I want the final result to be 我希望最终结果是
[{id=6140, Name=null},{id=6144, Name="Lion"}]
This is happening because the json you provided is not a json array, it is a json Object. 发生这种情况是因为您提供的json不是json数组,而是json对象。 you should do
你应该做
JsonParser json = new JsonParser();
JsonObject myJsonObject = json.parse(json).getAsJsonObject();
JsonArray jsonArray = myJsonObject.getAsJsonArray("Animals");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.