Converting JSON to Java object using Gson?

Up vote 0 down vote favorite share g+ share fb share tw.

I am trying to convert JSON string to simple java object but it is returning null. Below are the class details. JSON String: {"menu": {"id": "file", "value": "File", } } This is parsable class: public static void main(String args) { try { Reader r = new InputStreamReader(TestGson.class.

GetResourceAsStream("testdata. Json"), "UTF-8"); String s = Helper. ReadAll(r); Gson gson = new Gson(); Menu m = gson.

FromJson(s, Menu. Class); System.out. Println(m.getId()); System.out.

Println(m.getValue()); } catch (IOException e) { e.printStackTrace(); } } Below are th model class: public class Menu { String id; String value; public String getId() { return id; } public void setId(String id) { this. Id = id; } public String getValue() { return value; } public void setValue(String value) { this. Value = value; } public String toString() { return String.

Format("id: %s, value: %d", id, value); } } Everytime I am getting null. Can anyone please help me? Java android json gson link|improve this question edited Aug 1 '11 at 10:38Jonas12.2k741102 asked Aug 1 '11 at 10:23Avnish63 0% accept rate.

Try to make default counstructor ok tell if it worked – subspider Aug 1 '11 at 10:32 Check this for help. Stackoverflow.com/questions/6079505/… – Muhammad Zeeshan Aug 1 '11 at 10:33.

Your JSON is an object with a field menu. If you add the same in your Java it works: class MenuWrapper { Menu menu; public Menu getMenu() { return menu; } public void setMenu(Menu m) { menu = m; } } And an example: public static void main(String args) { String json = "{\"menu\": {\"id\": \"file\", \"value\": \"File\"} }"; Gson gson = new Gson(); MenuWrapper m = gson. FromJson(json, MenuWrapper.

Class); System.out. Println(m.getMenu().getId()); System.out. Println(m.getMenu().getValue()); } It will print: file File And your JSON: {"menu": {"id": "file", "value": "File", } } has an error, it has an extra comma.

It should be: {"menu": {"id": "file", "value": "File" } }.

Thanks, Jonas. But why did add Menu Wrraper class?...Do you have any document for this so that I can learn more...if you have please share it with me....thanks – Avnish Aug 2 '11 at 11:36 As I am new to this. I wanted to know is there any in built GSON in java or android for this converting JSON to java or vice -versa...as I know only JACKSON and GSON...please help me thanks... – Avnish Aug 2 '11 at 11:39.

What I have found helpful with Gson is to create an an instance of the class, call toJson() on it and compare the generated string with the string I am trying to parse.

{"menu": {"id": "file", "value": "File", } } replace it with "menu": {"id": "file", "value": "File", } ie. Public static void main(String args) { try { Reader r = new InputStreamReader(TestGson.class. GetResourceAsStream("testdata.

Json"), "UTF-8"); String s = Helper. ReadAll(r); s=s. Substring(1,s.

LastIndexOf("}"));` Gson gson = new Gson(); Menu m = gson. FromJson(s, Menu. Class); System.out.

Println(m.getId()); System.out. Println(m.getValue()); } catch (IOException e) { e.printStackTrace(); } }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions