JSON解析数据未显示在android textview中

JSON解析数据未显示在android textview中,android,Android,我有一个由php文件生成的json编码的消息。问题是我无法在文本视图中形成它,以便在android上显示它 package com.saki.json; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; import org.json.JSONObject; import org.json.JSONException; public class JsonActivity

我有一个由php文件生成的json编码的消息。问题是我无法在文本视图中形成它,以便在android上显示它

package com.saki.json;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import org.json.JSONObject;
import org.json.JSONException;

public class JsonActivity extends Activity {
private static final String JSON_STRING =
    "{\"person\":{\"name\":\"A\",\"age\":30,\"children\":[{\"name\":\"B\",\"age\":5}," + "\"name\":\"C\",\"age\":7},{\"name\":\"D\",\"age\":9}]}}";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TextView line1 = (TextView)findViewById(R.id.line1);
    TextView line2 = (TextView)findViewById(R.id.line2);
    TextView line3 = (TextView)findViewById(R.id.line3);
    try {
        JSONObject person = (new JSONObject(JSON_STRING)).getJSONObject("person");
        String name = person.getString("name");
        line1.setText("This person's name is " + name);
        line2.setText(name + " is " + person.getInt("age") + " years old.");
        line3.setText(name + " has " + person.getJSONArray("children").length()
            + " children.");
    } catch (JSONException e) {
        e.printStackTrace();
    }
}

如何解决这个问题?

正如Alex所说,JSON格式正确。我总是在使用它之前把我的用过。试试这个:

{\"person\":{\"name\":\"A\",\"age\":30,\"children\":[{\"name\":\"B\",\"age\":5},{\"name\":\"C\",\"age\":7},{\"name\":\"D\",\"age\":9}]}}

正如Alex所说,JSON格式正确。我总是在使用它之前把我的用过。试试这个:

{\"person\":{\"name\":\"A\",\"age\":30,\"children\":[{\"name\":\"B\",\"age\":5},{\"name\":\"C\",\"age\":7},{\"name\":\"D\",\"age\":9}]}}

您的JSON似乎不正确,您至少缺少了第二个子条目的开头括号
{
。应该会抛出一些异常,并在日志猫中使用标记
System.err
打印。您的JSON似乎不正确,您至少缺少开头括号
{
用于第二个子条目。应该会抛出一些异常,并在日志猫中使用标记
System.err
打印这些异常。