1

This is my json :

{
  "posts": [
    {    
      "id": "c200",
      "title": "erfan",
      "email": "erfan@gmail.com",
      "address": "xx-xx-xxxx,x - street, x - country",
      "gender" : "male",
  "attachments":[
  {
    "url":"http://memaraneha.ir/wp-content/uploads/2016/11/light.jpg"
  }]},
  {
    "id": "c201",
    "name": "Johnny Depp",
    "email": "johnny_depp@gmail.com",
    "address": "xx-xx-xxxx,x - street, x - country",
    "gender" : "male",
    "attachments":[
    {
      "url":"http://memaraneha.ir/wp-content/uploads/2016/11/renovate-old-home.jpg"}]
    }]
}

I want to get the title and url inside attachments array. I've tried this so far.

try {
    JSONObject jsonObj = new JSONObject(jsonStr);
    jsonContent jsonContent = new jsonContent(); //my class 

    JSONArray posts = jsonObj.getJSONArray("posts");

    for (int i = 0; i < posts.length(); i++) {
        JSONObject c = posts.getJSONObject(i);
        jsonContent.title = c.getString("title");

        JSONArray attachments=c.getJSONArray("attachments");

        for (int j=0;j<attachments.length();j++) {
            JSONObject a=attachments.getJSONObject(j);                     
            jsonContent.imgurl=a.getString("url");
        }

        listcontent.add(jsonContent);    //mylist
    }
} catch(Exception e) {}

Then I show the title and the url in a TextView , ImageView inside a RecyclerView. In card 1, I show the first title and image from the parsed json which works fine. But the problem is in card 2, which shows the title and image of card1 and doesn't matter how many json data we have, it just shows that same title and image in all items of my RecyclerView.

Here's the POJO for parsing the Json response.

public class jsonContent {
    public String title;
    public String imgurl;
}
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
Erfan
  • 3,059
  • 3
  • 22
  • 49
  • I don't know what `listcontent` is, but unless the `add` method makes a copy of its argument, you need a new `jsonContent` object for each post element. You're adding the same object for every post. – Ted Hopp Dec 15 '16 at 17:25
  • 1
    Possible duplicate of [How to parse JSON in Java](http://stackoverflow.com/questions/2591098/how-to-parse-json-in-java) – Selvin Dec 15 '16 at 17:28
  • @Selvin if u lookat my code its same its true but my problem different please read post charily then comment – Erfan Dec 15 '16 at 17:31
  • 1
    @PavneetSingh please do not answer if similar question was already answered you have enough rep to mark as duplicate ... and yes, it was bazillion times like [here](http://stackoverflow.com/questions/36257644/last-item-in-listobject-is-repeated-in-a-specific-textview-of-recyclerview-ite) , [here](http://stackoverflow.com/questions/6935350/listview-displaying-only-the-final-element-of-a-arraylisthashmap) – Selvin Dec 15 '16 at 17:35
  • @PavneetSingh - If you look at the time stamps, you'll see that I posted my comment before you posted your answer. Also, I doubt that this solves OP's problem, for reasons I explain in a comment to your answer. I also don't know what `listcontent.add()` does internally, and was hoping OP could clarify. – Ted Hopp Dec 15 '16 at 17:36
  • i edit my question and add jsoncontent class . but if u want help me answer my question and tell me where code must change not comment .thanks – Erfan Dec 15 '16 at 17:38
  • @TedHopp yah your comment is little ahead in time , my apologies and selvin i have no issue at marking it as a dupe – Pavneet_Singh Dec 15 '16 at 17:40
  • @TedHopp you think its duplicate question ? lookat my way its exact same way recommend in that link as you say duplicate my problem different if you want help me and you just give me correct answer not duplicate my question it not help me! – Erfan Dec 15 '16 at 17:47
  • he already did , plus you had the time to take a look at my code , you just need to create a new instance of `jsonContent ` in every iteration of your first loop instead of making a single one outside of your loop – Pavneet_Singh Dec 15 '16 at 17:49
  • 1
    I never said anything about a duplicate. That was Selvin. I suggested moving the line `jsonContent jsonContent = new jsonContent();` to inside the `for` loop. Have you tried that? – Ted Hopp Dec 15 '16 at 17:50
  • @PavneetSingh your answer gone! – Erfan Dec 15 '16 at 17:52
  • i gonna un-delete for some time for you to take a look – Pavneet_Singh Dec 15 '16 at 17:53
  • @TedHopp yes you right i do that and problem solve thanks for answer – Erfan Dec 15 '16 at 17:57
  • please un-accept my answer (i can't delete it otherwise) and you can accept the duplicate suggested – Pavneet_Singh Dec 15 '16 at 17:59
  • 1
    Pavneet "please do not answer" part was just a comment for your comment to Ted (which you deleted)... I didn't mean that you should really remove your answer... – Selvin Dec 15 '16 at 19:45
  • @Selvin got it , thanks – Pavneet_Singh Dec 16 '16 at 04:45

2 Answers2

1

you need to create new jsonContent object in every loop because currently you are just changing the values of same object in every iteration and adding it to the list.

 jsonContent jsonContent=null; //my class 

    JSONArray posts = jsonObj.getJSONArray("posts");
    for (int i = 0; i < posts.length(); i++) {
        jsonContent=new jsonContent(); //my class 
        //          ^^^^^^^^  create new object   
        JSONObject c = posts.getJSONObject(i);
        jsonContent.title=c.getString("title");

        JSONArray attachments=c.getJSONArray("attachments");
        for (int j=0;j<attachments.length();j++)
        {
            JSONObject a=attachments.getJSONObject(j);                     
            jsonContent.imgurl=a.getString("url");
        }
        listcontent.add(jsonContent);    //mylist
   }

Improvement : use getter and setter function instead of exposing your class fields as public

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
  • This doesn't explain the behavior OP is reporting. I agree that a new `jsonContent` is probably needed for each loop, but if that's really the problem, then the list would be filled with the **last** values read, whereas OP is reporting that only the first values are showing up for all posts. – Ted Hopp Dec 15 '16 at 17:33
  • @TedHopp could be OP indication was in general that both first and all values are same so this is the only issue with data and clearly indicate the behavior – Pavneet_Singh Dec 15 '16 at 17:35
  • @PavneetSingh just add this line jsonContent=new jsonContent(); in for loop and problem solve . i accept your answer please up vote my question selvin give me negetive vote :| – Erfan Dec 15 '16 at 17:58
0

I would suggest using Gson for parsing your json string. Its clean and easy to use.

In your case, you might have to create these classes.

public class jsonContent {
    public String id;
    public String title;
    public String email;
    public String address;
    public String gender;
    public Attachment[] attachments;
}

public class Attachment {
    public String[] url;
}

Now as your POJO are created, you are now set to use Gson to parse the json string you got there.

Gson gson = new Gson();
jsonContent[] postArray = gson.fromJson(jsonStr, jsonContent[].class);

Now pass this postArray to your adapter and iterate through the array to show the proper data in your TextView and ImageView in each item of your RecyclerView.

To add Gson in your Android project, you just have to add the following dependency in your build.gradle.

dependencies {
    compile 'com.google.code.gson:gson:2.4'
}
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98