0

Im working on an app where Im parsing JSON file and get the strings from it, but there is one String I have no idea why cant I get it into my activity. the String is Object > Array > String I have 2 activities and 1 model. MainActivity: where Im parsing the JSON. DetailActivity: where I need the String. PostModel: a model where I have all setter and getter.

JSON:

{  
   "status":"ok",
   "count":10,
   "count_total":184,
   "pages":19,
   "posts":[  
      {  },
      {  
         "id":2413,
         ,
         "categories":[  
            {  
               "id":100,
               "slug":"logging",
               "title":"logging",
               "description":"",
               "parent":0,
               "post_count":1
            }
         ],
        "comments":[  
            {  
               "id":3564,
               "content":"<p>\u47 <\/p>\n",
               "parent":0
            }
         ],
         "comment_count":1,
         "thumbnail":"http:\/\/www.5.com\/wtent\g",
         "custom_fields":{  
            "dsq_thread_id":[  
               "2365140"
            ],
            "videoID":[  
               "--ffwf92jvDFy"
            ]
         },
         "thumbnail_images":{  
            "full":{  
               "url":"http:\/\/www.5.com\/jpg",
               "width":727,
               "height":454
            },
            "thumbnail":{  
               "url":"http:\/\/www.5.com\/wp-con50.jpg",
               "width":150,
               "height":150
            }
         }
      }
   ]
}

PostModel:

    private List<VidCast> videoIDList;
    private String videoID;
    public String getVideoID() {
        return videoID;
    }

    public void setVideoID(String videoID) {
        this.videoID = videoID;
    }


    public List<VidCast> getvideoIDList() { return videoIDList; }

    public void setvideoIDList(List<VidCast> videoIDList) {
        this.videoIDList = videoIDList;
    }


    public static class VidCast {
        private String name;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }

}

MainActivity:

                    List<PostModel.VidCast> vidCasts = JsonPath.parse(URL_TO_HIT).read("$.posts.[*].custom_fields.[*].videoID[*]");
                    vidCasts = new ArrayList<>();
                    for (int s = 0 ; s < finalObject.getJSONArray("custom_fields").length() ; s++){
                        PostModel.VidCast vidCast = new PostModel.VidCast();
                        vidCast.setName(videoID);
                        vidCasts.add(vidCast);
                    }
                    postModel.setvideoIDList(vidCasts);

                    // adding the final object in the list
                    postModelList.add(postModel);
                }
                return postModelList;

            }
        }

DetailActivity:

StringBuffer stringBuffer = new StringBuffer();
            for(PostModel.CategoryCast categoryCast : postModel.getCategoryCastList()){
                stringBuffer.append(categoryCast.getName() + ", ");
            }

            StringBuffer videoStringBuffer = new StringBuffer();
            for(PostModel.VidCast videoIDList : postModel.getvideoIDList()) {
                videoStringBuffer.append(videoStringBuffer.toString());
            }

At the last file is where I need to get the <> String. I spent a lot of time I just cant figure it out how I can iterate over array inside an object.

Thanks in advance!

__________update________

I managed to parse it that way :

            JSONObject customFields = finalObject.getJSONObject("custom_fields");
            JSONArray vidCastsJson = customFields.getJSONArray("videoID");
            List<PostModel.VidCast> videoIds = new ArrayList<>();
            for (int s = 0 ; s < vidCastsJson.length() ; s++){
                PostModel.VidCast vidCast = new PostModel.VidCast();                  
                vidCast.setName(vidCastsJson.optString(s));
                videoIds.add(vidCast);
                String videoID = String.valueOf(vidCastsJson);
                vidCast.setName(videoID);

and I use Stringbuffer at DetailActivityStringBuffer

videoStringBuffer = new StringBuffer();
            for(PostModel.VidCast vidCast : postModel.getvideoIDList()) {
                videoStringBuffer.append(vidCast.getName());
                String videoID = vidCast.toString();
            }

But now I'm getting the videoID with the array brackets like that ["F3lyzrt"] I want it as a string to be only F3lyzrt, so I can pass it to my youtube player. Any advice will be appropriated.

Thanks,

Yasser
  • 1,058
  • 8
  • 6
  • your json is having format errors.please check that in any json formatter online tool like https://jsonformatter.curiousconcept.com/ – SriMaharshi Manchem Aug 07 '16 at 12:54
  • Yes I didnt put the whole code I putted the String I need with its array and object. – Yasser Aug 07 '16 at 14:54
  • Possible duplicate: https://stackoverflow.com/questions/9605913/how-to-parse-json-in-android/9606629#9606629 – nasch Aug 08 '16 at 13:53
  • @nasch doesn't look like my case, they explain how to get the JSON objects or arrays, but not both nested inside each other. – Yasser Aug 08 '16 at 16:47
  • It's no different if they're nested. You get an object or an array out of a JSONObject the same way whether it's contained in another JSONObject or not. – nasch Aug 08 '16 at 17:42
  • @nasch Thanks for your fast reply, but would you please, be more precise? It's my first time to try to parse Json, What I understood, I can iterate over a nested object/array as I iterate over a parent object/array? – Yasser Aug 08 '16 at 18:52

1 Answers1

0

It would look something like this:

JSONObject root = // however you get your root JSON object
        JSONArray posts = root.optJSONArray("posts");
        for(int i=0; i < posts.length(); i++){
            JSONObject post = posts.optJSONObject(i);
            int id = post.optInt("id");
            JSONArray categories = post.optJSONArray("categories");
            // etc.
        }

Though you might want to consider using GSON or Jackson. With those libraries you can define a model to represent the data (jsonschema2pojo.org can help with that) and then it does all the parsing for you.

EDIT

You're not even trying to get the video id. Here's your code:

for (int s = 0; s < finalObject.getJSONArray("videoID").length(); s++){
                             {
                              postModel.setVideoID(videoID);
                                 postModelList.add(postModel);
                            }

You see how you're not retrieving the contents of the json array?

JSONArray videoIds = finalObject.getJSONArray("videoID");
for (int s = 0; s < videoIds.length(); s++){

                          String videoID = videoIds.optString(s);
                          postModel.setVideoID(videoID);
                             postModelList.add(postModel);
                        }
nasch
  • 5,330
  • 6
  • 31
  • 52
  • Thanks, But as you can see at MainActivity and as I wrote the question, I cant get the string, if you can show me the way you mean, that would be great. – Yasser Aug 09 '16 at 20:40
  • I updated most of the code, I made what you suggest -or this what I think :-) - but still doesn't work, what Im doing wrong here? – Yasser Aug 11 '16 at 16:22
  • Update your question with current code. Pare it down to just the part you're having trouble with. And explain what "doesn't work" means. – nasch Aug 13 '16 at 08:08
  • Follow the code I posted and I think it will work. I'm not sure what you're trying to do with `String.valueOf(vidCastsJson)` but you don't need that. You've already set the name from the json array, so don't try to set it again. – nasch Aug 22 '16 at 12:56