[英]JSON parsing error in java using simle json parser
Let I have a string, json string. 让我有一个字符串,json字符串。
{"cond":{"to_email":"b@b.c"},"ret":"all"}
Now I want to parse it using json simple parser in java. 现在,我想在Java中使用json简单解析器对其进行解析。
I am giving the code... 我正在提供代码...
try{
//String s=request.getParameter("data");
String s="{\"cond\":{\"to_email\":\"b@b.c\"},\"ret\":\"all\"}";
JSONParser jsp=new JSONParser();
if(s == null || s.equals("")){
//problem
String json="{\"error\":\"error\",\"message\":\"no json data\"}";
response.getWriter().println(json);
}else{
JSONObject obj=(JSONObject) jsp.parse(s); //only object is allowed
JSONObject condObj=(JSONObject) jsp.parse(""+obj.get("cond"));
JSONObject returnObj=(JSONObject) jsp.parse(""+obj.get("ret"));
System.out.println(condObj);
}
Now the problem is that it's giving error... 现在的问题是它给出了错误...
Unexpected character (a) at position 0.
But if I remove the "ret" : "all"
then it's working well. 但是,如果我删除
"ret" : "all"
那么它运行良好。
Here in the example I printed condObj only but if I print retObj then it's giving null. 在此示例中,我仅打印了condObj,但是如果我打印retObj,则其为null。 So, the problem is the the
"ret" : "all"
part... 因此,问题出在
"ret" : "all"
部分...
But it's a correct json. 但这是一个正确的json。 I checked it.
我检查了 How to get out of this problem??
如何摆脱这个问题?
The thing is very simple! 事情很简单! The key "cond" represents an complex JSONObject but the key "ret" just a String.
键“ cond”表示一个复杂的JSONObject,而键“ ret”只是一个String。 So the parsing fails in this case.
因此在这种情况下解析失败。 I dont know which JSON-libary you are using, but have a look for an JSONObject#getString(String key) method to get the value.
我不知道您使用的是哪个JSON库,但是请查看JSONObject#getString(String key)方法来获取值。
Good luck 祝好运
UPDATE (with the JSON lib I use) 更新(使用我使用的JSON库)
try{
//String s=request.getParameter("data");
String s="{\"cond\":{\"to_email\":\"b@b.c\"},\"ret\":\"all\"}";
if(s == null || s.equals("")){
//problem
String json="{\"error\":\"error\",\"message\":\"no json data\"}";
}else{
JSONObject obj= new JSONObject(s);
JSONObject condObj=(JSONObject) obj.getJSONObject("cond");
String returnObj= obj.getString("ret");
System.out.println(condObj);
System.out.println(returnObj);
}
}
catch (Exception e) {
e.printStackTrace();
}
Just following the above answer ,here is a simple parser. 遵循以上答案,这是一个简单的解析器。
import java.util.Set;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class ParseJson {
public static void main(String[] args) throws Exception {
String s = "{\"cond\":{\"to_email\":\"b@b.c\"},\"ret\":\"all\"}";
JSONParser jsp = new JSONParser();
if (s == null || s.equals("")) {
String json = "{\"error\":\"error\",\"message\":\"no json data\"}";
} else {
JSONObject obj = (JSONObject) jsp.parse(s);
JSONObject condObj = (JSONObject) jsp.parse("" + obj.get("cond"));
Set<String> keys = obj.keySet();
for (String key : keys) {
System.out.println("Key : " + key);
System.out.print("Value : " +obj.get(key));
System.out.println();
}
}
}
}
This prints both the key and value pairs for you. 这会为您同时打印键和值对。 We can add conditionals for specific keys.
我们可以为特定键添加条件。 Key : ret Value : all Key : cond Value : {"to_email":"b@bc"}
键:ret值:全部键:cond值:{“ to_email”:“ b @ bc”}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.