Android “如何打印”;JSON";对象
请帮助将此类型打印为json格式?尝试此代码Android “如何打印”;JSON";对象,android,json,Android,Json,请帮助将此类型打印为json格式?尝试此代码 {"result":[{"heading":"The City School","text":"Established in 1978"}]} 用于漂亮的打印 String jsonStr = "{'result':[{'heading':'The City School','text':'Established in 1978'}]}"; try { JSONObject jsonObj = new JSONObject(jsonStr
{"result":[{"heading":"The City School","text":"Established in 1978"}]}
用于漂亮的打印
String jsonStr = "{'result':[{'heading':'The City School','text':'Established in 1978'}]}";
try {
JSONObject jsonObj = new JSONObject(jsonStr);
String headingVal=jsonObj.getDouble("heading");
String textVal=jsonObj.getDouble("text");
System.out.println("Heading --"+headingVal);
System.out.println("Text--"+textVal);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
检查回答:这已经是一个有效的JSON字符串。您在解析这个JSON字符串时有什么问题吗?您想打印什么?你想在哪里打印?具体一点。可能重复的
Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonParser jp = new JsonParser();
JsonElement je = jp.parse(uglyJSONString);
String prettyJsonString = gson.toJson(je);