2015-10-25 47 views
2

基本上我試圖做一個天氣應用程序。我試圖檢索一些數據(可以說crood對象的緯度和經度),但是每當我運行該應用程序時,它都不會顯示任何錯誤,但它會在logcat中顯示一個異常,其中顯示「沒有價值的cro」聲「。我嘗試了每個資源,但我無法將數據顯示到文字瀏覽。無法檢索json數據。如何檢索它

這就是我要想要的目標

{"coord":{"lon":-0.13,"lat":51.51},"weather":  [{"id":721,"main":"Haze","description":"haze","icon":"50d"},  {"id":501,"main":"Rain","description":"moderate rain","icon":"10d"}, {"id":311,"main":"Drizzle","description":"rain and drizzle","icon":"09d"}],"base":"stations","main":{"temp":286.15,"pressure":1013,"humidity":87,"temp_min":286.15,"temp_max":286.15},"visibility":10000,"wind":{"speed":3.1,"deg":200},"clouds":{"all":75},"dt":1445700000,"sys": {"type":1,"id":5089,"message":0.0153,"country":"GB","sunrise":1445668823,"sunset":1445705286},"id":2643743,"name":"London","cod":200} 

的JSON我這是怎麼做

  String finalJson=buffer.toString(); 

      JSONObject parentObject = new JSONObject(finalJson); 
      JSONObject jsonObject =parentObject.getJSONObject("crood"); 

如果有人幫我這個邏輯如何解析它。我想從這些數據中獲取緯度,經度,名稱,描述,溫度和壓力,並且我想在textviews中顯示它。

+2

這應該讀作 「座標」 *不* 「crood」 – t0mm13b

+0

你的對象說, 「座標」,但你寫的「crood模型」。 –

+0

是的,使用錯誤的鍵 –

回答

1

問題是與關鍵。我使用了錯誤的鑰匙。 :3 crood代替座標

+0

你最近看過Croods嗎? :D –

+0

哈哈是認真的:P –

0
  1. 創建模型,聲明你想要設置和獲取。
  2. 接下來創建arrayadapter並定位您的對象。
  3. 創建活動申報

    HttpClient的客戶=新DefaultHttpClient();

      HttpGet request = new HttpGet("Url Json Url"); 
    
          HttpResponse response = client.execute(request); 
    
          HttpEntity resEntity = response.getEntity(); 
    
          String json = EntityUtils.toString(resEntity); 
    
          JSONArray jarray = new JSONArray(json); 
    
          newsList = new ArrayList<NewsModel>(); 
    
          for (int i = 0; i < jarray.length(); i++) 
    

    {

       JSONObject row = jarray.getJSONObject(i); 
    
           NewsModel data = new NewsModel(); 
    
           data.setTitle(row.getString("title")); 
    
           data.setContent(row.getString("content")); 
    
           data.setExcerpt(row.getString("excerpt")); 
    
           data.setDate(row.getString("date")); 
    
           data.setImage(row.getString("feature_image")); 
    
           newsList.add(data); 
    
+0

我遇到了建模問題。我不知道該怎麼做。是的,我知道但我很困惑。如果你可以指導建模,這將是很大的幫助 –

+0

你可以分享你的完整數據 –

+1

首先使用jsonlint.com檢查vaild json或ont。 –

0

創建低於

ublic class NewsModel implements Serializable{ 

String title; 

public String getTitle() { 
    return title; 
} 

public void setTitle(String title) { 
    this.title = title; 
} 

public String getContent() { 
    return content; 
} 

public void setContent(String content) { 
    this.content = content; 
} 

public String getExcerpt() { 
    return excerpt; 
} 

public void setExcerpt(String excerpt) { 
    this.excerpt = excerpt; 
} 

public String getDate() { 
    return date; 
} 

public void setDate(String date) { 
    this.date = date; 
} 

public String getImage() { 
    return image; 
} 

public void setImage(String image) { 
    this.image = image; 
} 

String content; 
String excerpt; 
String date; 
String image; 

}