2011-11-09 34 views
-1

我得到了一些來自服務器端的響應,我需要解析但不是通過名稱,即響應會隨時變化。android:json異常

type = "data_updates"; 
     updates =    { 
      "offers_all" =     { 
       columns =      (
        "record_id", 
        "display_name", 
        "sorting_name", 
        latitude, 
        longitude, 
        "custom_ad_banner_resource_id", 
        description, 
        "website_url", 
        "phone_number", 
        hours, 
        advertiser, 
        "street_address", 
        "offer_id", 
        "offer_section", 
        "start_date", 
        "end_date", 
        "custom_leaf_template", 
        "location_area" 
       ); 
       data =      (
              (
         "09834-234234", 
         "The Example Offer", 
         "example offer, the", 
         "-122.234234", 
         "31.2342343", 
         "", 
         "An Example of an Offer.", 
         "http=>//freeruntech.com/", 
         "1.707.555.1212", 
         "10am - 5pm", 
         Yes, 
         "3145 State Street,\nCalistoga, CA, 94500", 
         "", 
         "", 
         "2011-08-24", 
         "2011-12-31", 
         "", 
         Napa 
        )} 

我用下面的代碼獲取主一個JSONObjects

if (show.get("type").equals("data_updates")) 
{ 
    JSONObject data_updates = show.getJSONObject("updates"); 

    JSONArray array_containing_values= new JSONArray(); 
    JSONArray data_updates_values=data_updates.names(); 

    System.out.println(data_updates_values); 
    for(int m=0; m<data_updates_values.length(); m++) 
    { 
     JSONObject obj = new JSONObject((String) data_updates_values.get(m)); 
     JSONArray names = new JSONArray(); 
     names= obj.toJSONArray(names);     
     System.out.println(names); 
    } 
} 

但它給我的異常

Value events of type java.lang.String cannot be converted to JSONObject 

我應如何獲得數據和列JSonobjects的值。

任何人都可以幫助我嗎?

謝謝

+1

它看起來不是有效的JSON。你確定你得到了JSON嗎? –

+0

雅我相信它只是json – Aditya1510

+0

@Nikita其實我在這裏寫了一部分,其實反應非常大。 – Aditya1510

回答

0

你是不是這樣做?

HttpEntity entity = response.getEntity(); 

      if (entity != null) { 
       // Read the content stream 
       InputStream instream = entity.getContent(); 
       Header contentEncoding = response.getFirstHeader("Content-Encoding"); 
       if (contentEncoding != null && contentEncoding.getValue().equalsIgnoreCase("gzip")) { 
        instream = new GZIPInputStream(instream); 
       } 

       // convert content stream to a String 
       String resultString= convertStreamToString(instream); 
       Log.v(null, "resultString "+resultString); 
       instream.close(); 


       // Transform the String into a JSONObject 
       if(resultString!=null){ 
        jsonObjRecv = new JSONObject(resultString); 

       } 

      // Raw DEBUG output of our received JSON object: 
      Log.i(TAG,"<jsonobject>\n"+jsonObjRecv.toString()+"\n</jsonobject>"); 

你會得到這樣的回答嗎?

如果是的話。這裏resulttring將'秀'嘗試

JSONObject jsshow = new JSONObject(show) 
1

這不是一個有效的json是問題。在json =中被替換爲:,並且數組的值以[開始並且結束於],沒有;在json中。看看下面的鏈接..

http://www.json.org/