importnet.sf.json.JSONObject;publicclassqianbao{publicstaticvoidmain(String[]args){StringjsonStr="{'name':111,'child':[{'name':222,'child':[{'name':333}]},{'name'...
import net.sf.json.JSONObject;
public class qianbao {
public static void main(String[] args) {
String jsonStr = "{'name':111,'child':[{'name':222,'child':[{'name':333}]},{'name':2221}]}";
System.out.println(jsonStr);
JSONObject jsonObj = JSONObject.fromObject(jsonStr.toString());
String a = jsonObj.getString("name");
System.out.println(a);
//JSONArray jsonArray = jsonObj.getJSONArray("child");
//if (null != jsonArray && jsonArray.size() > 0) {
//
//for (int i = 0; i < jsonArray.size(); i++) {
//
//Object obj = jsonArray.get(i);
//
//JSONObject json = JSONObject.fromObject(obj);
//
//if (StringUtils.isNotBlank(json.getString("name"))) {
//
//System.out.println("name " + json.getString("name"));
//
//}
//
//}
//}
}
}
运行后报错
{'name':111,'child':[{'name':222,'child':[{'name':333}]},{'name':2221}]}
Exception in thread "main" net.sf.json.JSONException: A JSONObject text must begin with '{' at character 1 of {'name':111,'child':[{'name':222,'child':[{'name':333}]},{'name':2221}]}
at net.sf.json.util.JSONTokener.syntaxError(JSONTokener.java:527)
at net.sf.json.JSONObject._fromJSONTokener(JSONObject.java:1112)
at net.sf.json.JSONObject._fromString(JSONObject.java:1337)
at net.sf.json.JSONObject.fromObject(JSONObject.java:187)
at net.sf.json.JSONObject.fromObject(JSONObject.java:156)
at qianbao.main(qianbao.java:19)
哪位大神帮看看,这种的string应该如何解析?
展开