Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android “如何打印”;JSON";对象_Android_Json - Fatal编程技术网

Android “如何打印”;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

请帮助将此类型打印为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);
    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);