0

I'm having a little trouble working out an appropriate java object structure for the following JSON data:

"pages": {
            "181382": {
                "pageid": 181382,
                "ns": 0,
                "title": "Anonymity"
            },
            "7181837": {
                "pageid": 7181837,
                "ns": 0,
                "title": "Anonymous"
            }
        }

The identifiers "181382" and "7181837" change depending on the data returned so these cannot be used as a member on an object. I tried to approach it using a Map<String, Object> approach but got a little stuck.

Edit: This is what I've tried

public class PageData {

    int pageid;
    String ns;
    String title;

    public int getPageid() {
        return pageid;
    }

    public String getNs() {
        return ns;
    }

    public String getTitle() {
        return title;
    }

}



Map<String, PageData> pages = results.getPages().getData();

        for (PageData data : pages.values()) {
            System.out.println(data.getTitle());
        }
5
  • Where did you get stuck? Show us how you tried to map it. You can map the object named 7181837 to a POJO and use a Map<String, YourPojo>. Commented Jan 9, 2014 at 21:14
  • Well it just wasn't mapping correctly so it threw null pointers when I tried to access any of the data Commented Jan 9, 2014 at 21:15
  • So post the actual JSON, actual DTOs/POJOs, and actual usage of such. Chance are a level if "off". Commented Jan 9, 2014 at 21:15
  • Seems to me it's a Map containing two more Maps. If you wish you can replace the inner Maps with Page objects, but there's no compelling reason to do so. Commented Jan 9, 2014 at 21:18
  • class PageCollection { public Map<String, PageData> pages; } might be useful .. chances are you're deserializing off by a level as the JSON Object related to the Map is the value of the pages property. (Also, you should still show the usage of the GSON). Commented Jan 9, 2014 at 21:18

1 Answer 1

1

Just create some wrapper for your Object. Here is working example:

Wrapper

public class Wrapper {
    Map<String, PageData> pages = null;

    public Map<String, PageData> getPages() {
        return pages;
    }   
}

Launcher

public class Launcher {
    public static void main(String[] args) {
        String str = "{\"pages\": {\r\n" + 
                "            \"181382\": {\r\n" + 
                "                \"pageid\": 181382,\r\n" + 
                "                \"ns\": 0,\r\n" + 
                "                \"title\": \"Anonymity\"\r\n" + 
                "            },\r\n" + 
                "            \"7181837\": {\r\n" + 
                "                \"pageid\": 7181837,\r\n" + 
                "                \"ns\": 0,\r\n" + 
                "                \"title\": \"Anonymous\"\r\n" + 
                "            }\r\n" + 
                "        }" +
                "}";


        Gson gson = new Gson();

        Wrapper results = gson.fromJson(str, Wrapper.class);

        Map<String, PageData> pages = results.getPages();

        for (PageData data : pages.values()) {
            System.out.println(data.getTitle());
        }
    }
}

PageData

public class PageData{/* the same */}

Output:

Anonymity
Anonymous
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.