Android 将json字符串保存到arraylist
下面的JSONObject格式字符串存储在arraylist ArrayListAtestNews中 response.toString()是 最新消息Android 将json字符串保存到arraylist,android,arraylist,jsonobject,Android,Arraylist,Jsonobject,下面的JSONObject格式字符串存储在arraylist ArrayListAtestNews中 response.toString()是 最新消息 public class LatestNews { public String title; public String info; public String link; public String image; public String pubDate; public String gui
public class LatestNews {
public String title;
public String info;
public String link;
public String image;
public String pubDate;
public String guid;
public LatestNews (String title_,String info_,String link_,String image_,String pubDate_,String guid_){
this.title=title_;
this.info=info_;
this.link=link_;
this.image=image_;
this.pubDate=pubDate_;
this.guid=guid_;
}
}
我提取json如下
JSONObject obj = new JSONObject(response.toString());
JSONArray venues = obj.getJSONArray("blue");
System.out.println("response object "+venues.length());
for (int x=0;x<venues.length();x++){
JSONObject keyValue = venues.getJSONObject(x);
if (keyValue.has("title")){
System.out.println("xxxtitle "+keyValue.getString("title"));
}
}
JSONObject obj=新的JSONObject(response.toString());
JSONArray场馆=obj.getJSONArray(“蓝色”);
System.out.println(“响应对象”+victions.length());
对于(intx=0;x首先和大多数情况,您的JSON响应不符合标准和优化的响应
实现您的web服务逻辑以获得以下格式的响应,这将帮助您减少解析逻辑/代码和迭代次数,这也将帮助您提高应用程序性能
{
"blue": [
{
"title": "http:\\www.b",
"info": " http:\\www.b",
"link": "http:\\www.b",
"image": "http:\\www.b",
"pubDate": "http:\\www.b",
"guid": "http:\\www.b"
},
{
"title": "http:\\www.b",
"info": " http:\\www.b",
"link": "http:\\www.b",
"image": "http:\\www.b",
"pubDate": "http:\\www.b",
"guid": "http:\\www.b"
}
]
}
现在,关于JSON解析教程/代码,在web和Stackoverflow上也有很多示例,请看:
代码和JSON格式/逻辑中的错误:
正如您所说,只打印第一个标题,其原因是您已经编写了obj.getJSONArray(“blue”);
,这将只为您提供第一个“blue”对象,因此它将为您提供来自blue对象的标题值
我已经在上面建议了一个优化的解决方案!@blackbelt不应该,因为我正在更新更详细的答案。我已经知道规则。谢谢:)我知道你知道规则。。无论如何,如果问题是格式错误的JSON,那么对象的创建就会失败,我认为..@blackbelt上面的答案不可能适用于comment@blackbelt只需检查更新的答案和错误细节。
{
"blue": [
{
"title": "http:\\www.b",
"info": " http:\\www.b",
"link": "http:\\www.b",
"image": "http:\\www.b",
"pubDate": "http:\\www.b",
"guid": "http:\\www.b"
},
{
"title": "http:\\www.b",
"info": " http:\\www.b",
"link": "http:\\www.b",
"image": "http:\\www.b",
"pubDate": "http:\\www.b",
"guid": "http:\\www.b"
}
]
}