1 Replies - 126 Views - Last Post: 02 July 2019 - 04:06 AM Rate Topic: -----

#1 dipak_007   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 30-June 19

Convert Java Object POJO to nested JSON list

Posted 01 July 2019 - 08:04 PM

Help me to parse below JSON format from POJO. I am using Jaskson 2.x
{
  "summary" : "Summary goes here.",
  "custom_fields": [
	{
		"field": {
			"name": "Custom Field 1"
		},
		"value": "Seattle"
	}
  ]
}


I have tried something like this.
public class ModelExample {

	public ModelExample() {}
	
	private String summary;
	private List<Custom_fields> customFields;
	
	//getter and setter
}


public class Custom_fields
{
	public Custom_fields() {}
	
	private Map<String, String> field;
    private String value;
	
	//getter and setter
}


public class Issues {

	public Issues() {
	}
	
	private static ModelExample modelExample = new ModelExample();
	private static Custom_fields custom_Fields;
	
	public static ModelExample example() {
	Map<String, String> mapCustomValues = new HashMap<String, String>();
	mapCustomValues.put("name", "Custom Field 1");
	custom_Fields.setField(mapCustomValues);
	custom_Fields.setValue("Seattle");
	
//	modelExample.setCustomFields(custom_Fields); here i am getting this error.
	return modelExample;
	}
}


public class ExampleMain {

	public static void main(String[] args) {
		ObjectMapper objectmapper = new ObjectMapper();
		objectmapper.writerWithDefaultPrettyPrinter().writeValueAsString(Issues.example());
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Convert Java Object POJO to nested JSON list

#2 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3638
  • View blog
  • Posts: 16,706
  • Joined: 20-September 08

Re: Convert Java Object POJO to nested JSON list

Posted 02 July 2019 - 04:06 AM

Please post the class definition of the real object. Discussing hypothetical ones is pointless
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1