2013-08-31 48 views
0

當我試圖解析這個JSON時,我似乎錯過了一些東西。我繼續得到一個沒有價值的項目JSON錯誤。 (請參閱下面的logcat)。JSON PARSE沒有值

下面是我從App Engine數據存儲拉動JSON:

{"items":[{"description":"payment confirmed","emailAddress":"[email protected]","id":"Fri Aug 30 17:20:35 EDT 2013","nameFirst":"Bill","nameLast":"Sanders","phone":"5555555555","state":"KS","streetAddress":"123 Rick Dr","zipCode":"44444","kind":"contactinfoendpoint#resourcesItem"},{"description":"payment confirmed","emailAddress":"[email protected]","id":"Fri Aug 30 17:21:37 EDT 2013","nameFirst":"Frank","nameLast":"Lloyd","phone":"5554567896","state":"KY","streetAddress":"999 Rock St","zipCode":"44555","kind":"contactinfoendpoint#resourcesItem"}],"kind":"contactinfoendpoint#resources","etag":"\"NiDXI3T89oRrAPGJAhgGQje1Z0w/z0W4p01mjHc5cNgcOt3Kb_1xo8E\""} 

當我運行這段代碼我在logcat中得到這個和我的佈局充氣空白:

08-31 11:11:37.470: E/log_tag(15026): Error parsing data org.json.JSONException: No value for item 

這裏是活動的代碼:

public class FinderActivity extends ListActivity { 


private static final String TAG_ID = "id"; 
private static final String TAG_FIRSTNAME = "nameFirst"; 
private static final String TAG_LASTNAME = "nameLast"; 
private static final String TAG_EMAIL = "emailAddress"; 
private static final String TAG_ADDRESS = "address"; 
private static final String TAG_STATE = "state"; 

private static final String TAG_PHONE = "phone"; 
JSONArray contacts = null; 

ArrayList<HashMap<String, String>> contactList = new ArrayList<HashMap<String, String>>(); 

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

    new EndpointsTask().execute(getApplicationContext()); 

} 

public class EndpointsTask extends AsyncTask<Context, Integer, Long> { 

    public Long doInBackground(Context... contexts) { 

     Contactinfoendpoint.Builder endpointBuilder = new Contactinfoendpoint.Builder(
      AndroidHttp.newCompatibleTransport(), 
      new JacksonFactory(), 
      new HttpRequestInitializer() { 
      public void initialize(HttpRequest httpRequest) { } 
      }); 
    Contactinfoendpoint endpoint = CloudEndpointUtils.updateBuilder(
    endpointBuilder).build(); 

    try { 

    // final TextView detail = (TextView)findViewById(R.id.textView100); 

    String apples = endpoint.listContactInfo().execute().toString(); 

    JSONObject jObject = new JSONObject(apples); 

      try{ 
     //Get the element that holds the earthquakes (JSONArray) 
     JSONArray contacts = jObject.getJSONArray("item"); 

     for(int i = 0; i < contacts.length(); i++){ 
      JSONObject c = contacts.getJSONObject(i); 

      // Storing each json item in variable 
      String id = c.getString(TAG_ID); 
      String nameFirst = c.getString(TAG_FIRSTNAME); 
      String nameLast = c.getString(TAG_LASTNAME); 
      String email = c.getString(TAG_EMAIL); 
      String address = c.getString(TAG_ADDRESS); 
      String phone = c.getString(TAG_PHONE); 

      // creating new HashMap 
      HashMap<String, String> map = new HashMap<String, String>(); 

      // adding each child node to HashMap key => value 
      map.put(TAG_ID, id); 
      map.put(TAG_FIRSTNAME, nameFirst); 
      map.put(TAG_EMAIL, email); 
      map.put(TAG_PHONE, phone); 

      // adding HashList to ArrayList 
      contactList.add(map); 

     } 

      }catch(JSONException e)  { 
      Log.e("log_tag", "Error parsing data "+e.toString()); 
      } 

      ListAdapter adapter = new SimpleAdapter(FinderActivity.this, contactList, 
        R.layout.main, 
        new String[] { TAG_FIRSTNAME, TAG_EMAIL, TAG_PHONE }, new int[] { 
          R.id.name, R.id.email, R.id.mobile }); 

      setListAdapter(adapter); 

      // selecting single ListView item 
      ListView lv = getListView(); 

    } catch (IOException e) { 
    e.printStackTrace(); 
    } catch (JSONException e) { 
    // TODO Auto-generated catch block 
    e.printStackTrace(); 
} 
     return (long) 0; 
    } 


    } 
+0

什麼是異常的完整堆棧跟蹤?它幾乎肯定會告訴你它試圖找到哪個值。 – MrLore

+2

看起來應該是jObject.getJSONArray(「item」)中的「items」而不是「item」; –

回答

1

你的對象名稱是不是itemitems

使用下面的代碼來獲得項目

JSONObject jsonObj = new JSONObject(str); 
JSONArray jsonArr = jsonObj.getJSONArray("items"); 

for(int i =0 ; i<jsonArr.length() ;i++){ 
    JSONObject jsonObj1 = jsonArr.getJSONObject(i); 

    System.out.println(); 
    System.out.println((i+1)+" ==========JSON OBJECT IS :========== "); 

    Iterator<String> iter = jsonObj1.keys(); 
    while(iter.hasNext()){ 
     String key = iter.next(); 
     System.out.println(key + " : " + jsonObj1.get(key)); 


    } 
} 

OUTPUT:

1 ==========JSON OBJECT IS :========== 
streetAddress : 123 Rick Dr 
id : Fri Aug 30 17:20:35 EDT 2013 
phone : 5555555555 
nameLast : Sanders 
nameFirst : Bill 
description : payment confirmed 
zipCode : 44444 
state : KS 
emailAddress : [email protected] 
kind : contactinfoendpoint#resourcesItem 

2 ==========JSON OBJECT IS :========== 
streetAddress : 999 Rock St 
id : Fri Aug 30 17:21:37 EDT 2013 
phone : 5554567896 
nameLast : Lloyd 
nameFirst : Frank 
description : payment confirmed 
zipCode : 44555 
state : KY 
emailAddress : [email protected] 
kind : contactinfoendpoint#resourcesItem 
+0

我需要做什麼才能讓Iterator iter將每條聯繫人信息存儲在如下字符串中:String id = c.getString(TAG_ID); String nameFirst = c.getString(TAG_FIRSTNAME); ,這樣對於每個JSON對象,我都會得到一組聯繫信息,並將其分成每個電話號碼,名稱等字符串。 – johnsonjp34

0

上面的json對象包含items.So您需要首先解析「項目」,然後解析項目內的內容。

1

是的。首先需要獲取Array element,然後獲取數組的元素。

JSONParser parser=new JSONParser(); 

JSONObject jsonObject = (JSONObject)parser.parse(apples); 

JSONArray jsonArr = (JSONArray)jsonObject.get("items"); 

System.out.println("jsonArr.get(0));