在android中解析JSON中数组内的数组时出现问题
我必须得到用户发布的图像,这是一个数组 在JSON的主数组中。当我试图得到那个图像时,我的 Logcat显示在android中解析JSON中数组内的数组时出现问题,android,json,Android,Json,我必须得到用户发布的图像,这是一个数组 在JSON的主数组中。当我试图得到那个图像时,我的 Logcat显示tweet\u图像中没有值 我的JSON是这样的:- {"feed": [ {"tweet_id":"800", "userid":"11", "content":"null", "favorite_count":"0", "reply_count":"0",
tweet\u图像中没有值
我的JSON是这样的:-
{"feed":
[
{"tweet_id":"800",
"userid":"11",
"content":"null",
"favorite_count":"0",
"reply_count":"0",
"retweet_count":"0",
"tweet_location":"",
"created_date":"2015-06-16 16:20:16",
"name":"devraj singh",
"user_image":"http:\/\/sabakuch.com\/public\/images_upload\/avatars\/ozone\/11_30_11149322_10205781000956557_74437511307260696_n.jpg",
"tweet_images":null,
"gender":"1"},
{"tweet_id":"794",
"userid":"6",
"content":"<a href=http:\/\/www.punjabkesari.in\/news\/article-370994>http:\/\/www.punjabkesari.in\/news\/article-370994<\/a>",
"favorite_count":"0",
"reply_count":"0",
"retweet_count":"0",
"tweet_location":"",
"created_date":"2015-06-16 11:49:00",
"name":"amar bhanu",
"user_image":"http:\/\/sabakuch.com\/public\/images_upload\/avatars\/ozone\/6_30_imageamar.jpg",
"tweet_images":{"image":["http:\/\/sabakuch.com\/public\/images_upload\/tweet\/794_400_1434435540_album143443554069.jpg"]},
"gender":"1"}
]}
请告诉我我犯了什么错误?你可以这样做
try {
JSONObject _jObject = new JSONObject("YOUR_JSON_STRING");
JSONArray _jArrayFeed = _jObject.getJSONArray("feed");
if (_jArrayFeed.length()>0) {
for (int i = 0; i < _jArrayFeed.length(); i++) {
JSONObject _subObj = _jArrayFeed.getJSONObject(i);
String _tweet_id = _subObj.getString("tweet_id");
String _userid = _subObj.getString("userid");
String _content = _subObj.getString("content");
String _favorite_count = _subObj.getString("favorite_count");
String _reply_count = _subObj.getString("reply_count");
String _retweet_count= _subObj.getString("retweet_count");
String _tweet_location = _subObj.getString("tweet_location");
String _created_date = _subObj.getString("created_date");
String _name = _subObj.getString("name");
String _user_image = _subObj.getString("user_image");
String _tweet_images = _subObj.getString("tweet_images");
String _gender = _subObj.getString("gender");
}
}
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
试试看{
JSONObject_jObject=新的JSONObject(“您的JSON字符串”);
JSONArray _jArrayFeed=_jObject.getJSONArray(“feed”);
如果(_jArrayFeed.length()>0){
对于(int i=0;i<_jArrayFeed.length();i++){
JSONObject SubObject=jArrayFeed.getJSONObject(i);
String_tweet_id=_subObj.getString(“tweet_id”);
字符串_userid=_subObj.getString(“userid”);
字符串_content=_subObj.getString(“内容”);
String _favorite_count=_subObj.getString(“favorite_count”);
String_reply_count=_subObj.getString(“reply_count”);
String _retweet_count=_subObj.getString(“retweet_count”);
String_tweet_location=_subObj.getString(“tweet_location”);
String_created_date=_subObj.getString(“created_date”);
字符串_name=_subObj.getString(“名称”);
String_user_image=_subObj.getString(“user_image”);
String_tweet_images=_subObj.getString(“tweet_images”);
字符串_性别=_subObj.getString(“性别”);
}
}
}捕获(JSONException e1){
//TODO自动生成的捕捉块
e1.printStackTrace();
}
tweet\u图像是一个JSonObject
JSonObject:
包含命名值(键->值对、元组)
身份证号码:1
元素的顺序并不重要
{id:1,name:'B'}的JSONObject等于{name:'B',id:1}
杰索纳雷:
仅包含系列值
像[1,'值']
价值顺序很重要
[1,'value']的数组与['value',1]不同JSONObject json=jsonParser.makeHttpRequest(url,“GET”,params);
JSONObject json = jsonParser.makeHttpRequest(url, "GET",params);
Log.d("general JSON ", json.toString());
try {
JSONObject inbox = json.getJSONArray("feed");
// looping through All Feed
for (int i = 0; i < inbox.length(); i++) {
JSONObject c = inbox.getJSONObject(i);
String tweet_id = c.getString("tweet_id");
String userid = c.getString("userid");
String content = c.getString("content");
.
.
.
.
JSONObject tweet_images = c.getJSONObject("tweet_images");
if( tweet_images != null ){
JSONObject image = tweet_images.getJSONArray("image");
for(int i=0; i<image.length(); i++){
String img = image.getString(i)
}
}
}
Log.d(“general JSON”,JSON.toString());
试一试{
JSONObject inbox=json.getJSONArray(“提要”);
//循环通过所有馈送
对于(int i=0;i 对于(inti=0;i您是否尝试过使用谷歌的GSON
以下是该项目的链接:
下载页面:
以及解析JSON的示例代码:
Gson gson = new Gson();
YourObject objects = gson.fromJson(jsonString, YourObject.class);
您的对象
应该如下所示:
public class YourObject{
Feed[] feed;
}
和提要
类:
public class Feed{
int tweet_id;
int userid;
...
}
请注意:如果任何Json元素都有null或空值,即使它们主要是Feed
类中的int,最好将它们声明为String
,以避免java.lang.NumberFormatException
tweet\u图像是一个JSONObject,而不是JSONArray。正如他们所说,tweet\u图像是一个JSONObject,并且我真的可以推荐你使用某种json到对象映射器。看看GSON或Jackson。让生活变得更简单:)请查看答案,并让我们知道哪一个是有用的。@ShoebSiddique您认为我是堆栈新手还是不知道如何使用它?我正在尝试找出解决方案。顺便说一下,您的答案不起作用,因为我使用的是适配器类,所以我只能获取其中的值。我也应该投票否决您的答案,因为它是是完全错误的方式,因为你得到的是“tweet_图像”而不是“图像”在这里面。现在你能告诉我你为什么否决我的问题吗?很抱歉回答得太晚。我尝试了你的解决方案,但问题是,我已经创建了bean类和自定义适配器类,所以当我从构造函数中删除tweet_图像标记时,它在bean类构造函数中给出了错误。
public class YourObject{
Feed[] feed;
}
public class Feed{
int tweet_id;
int userid;
...
}