2011-09-20 50 views
2

我有一個json編碼的消息,它是由php文件生成的。問題是我無法在textview中將其顯示在android上。JSON解析的數據不顯示在android textview

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(); 
    } 
} 

我該如何解決這個問題?

+2

你的JSON似乎並不正確,你至少錯過了第二個子項的開頭括號「{」。應該會拋出一些異常,並在logcat中使用TAG'System.err'打印。 – 2011-09-20 22:57:20

回答