解决在android中的JSON中的Array内部的问题

时间:2015-06-16 13:50:44

标签: android json

  

我必须得到用户发布的图像   在JSON的主数组中。当我想要获得那个图像时,我的    Logcat 表示tweet_image中没有值。

我的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"}

]}

这是我试图获取图像的方式..

JSONObject json = jsonParser.makeHttpRequest(url, "GET",params);
            Log.d("general JSON ", json.toString());
            try {
                inbox = json.getJSONArray(TAG_FEED);
                inboxImage = json.getJSONArray(TAG_TWEET_IMAGE);
                // looping through All messages
                for (int i = 0; i < inbox.length(); i++) {
                    JSONObject c = inbox.getJSONObject(i);

                    for(int j=0; j < inboxImage.length();j++){
                    JSONObject d = inboxImage.getJSONObject(j);

                    Ozone_Beans ozonebean = new Ozone_Beans(c.getString(TAG_CONTENT), c.getString(TAG_NAME),
                            c.getString(TAG_DATE),c.getString(TAG_USER_IMAGE),c.getString(TAG_TWEET),c.getString(TAG_USER),
                            d.getString(TAG_TWEET_IM));
  

我将值放在自定义适配器类中的视图中   这个..

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View vi = convertView;
        ViewHolder holder;

        if(convertView==null)

            convertView = inflater.inflate(R.layout.ozone_item, null);
        holder = new ViewHolder();


        holder.tvUser = (TextView)convertView.findViewById(R.id.ozone_name);
        holder.wbvContent = (WebView)convertView.findViewById(R.id.ozone_content1);
        holder.tvDate = (TextView)convertView.findViewById(R.id.ozone_date);
        holder.image_user_image = (ImageView)convertView.findViewById(R.id.ozone_user_image);
        holder.postImage = (ImageView)convertView.findViewById(R.id.ozone_post_image);
        //holder.replyEdittext = (EditText)convertView.findViewById(R.id.ozone_writesomething);

    final String S = feedList.get(position).getUserid();

            holder.tvUser.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                // TODO Auto-generated method stub
                Intent reintent = new Intent(context,Ozone_ProfileUserList.class);

                reintent.putExtra("userid", S);
                context.startActivity(reintent);
            }
        }); 


        holder.tvUser.setText(feedList.get(position).getName());
        holder.tvDate.setText(Character.toUpperCase((feedList.get(position).getCreated_date()).charAt(0))+(feedList.get(position).getCreated_date()).substring(1));
        holder.wbvContent.loadData(feedList.get(position).getContent(), "text/html; charset=UTF-8", null);

         imageloader.displayImage(feedList.get(position).getUser_image(), holder.image_user_image);
         imageloader1.displayImage(feedList.get(position).getTweet_image(), holder.postImage);

        return convertView;

    }

请让我知道我在这里犯了什么错误?

4 个答案:

答案 0 :(得分:1)

tweet_image是一个JSonObject

的JSONObject: 包含命名值(键 - >值对,元组) 喜欢ID:1 元素的顺序并不重要 {id:1,name:'B'}的JSONObject等于{name:'B',id:1}。

JSONArray: 仅包含系列值 比如[1,'value'] 价值秩序很重要 [1,'value']的数组与['value',1]

不同

答案 1 :(得分:0)

你可以做这样的事情

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();
                    }

答案 2 :(得分:0)

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)
                    }

                 }

               }

}

答案 3 :(得分:0)

您是否尝试过使用Google的GSON?

以下是该项目的链接:https://github.com/google/gson

下载页面:http://search.maven.org/#artifactdetails%7Ccom.google.code.gson%7Cgson%7C2.3.1%7Cjar

用于解析JSON的示例代码:

Gson gson = new Gson();
YourObject objects = gson.fromJson(jsonString, YourObject.class);

YourObject应该是这样的:

public class YourObject{
    Feed[] feed;
}

Feed类:

public class Feed{
    int tweet_id;
    int userid;
    ...
}

只是单挑:如果任何Json元素的值为null或空值,即使它们主要是Feed类中的int,最好将它们声明为{{1}避免String