2015-10-25 123 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; 

}