How to read a array value form json in java - java

I have following json data:-
{
"store": {
"book": [
{
"category": "reference",
"author": "Nigel Rees",
"title": "Sayings of the Century",
"price": 8.95
},
{
"category": "fiction",
"author": "Evelyn Waugh",
"title": "Sword of Honour",
"price": 12.99
},
{
"category": "fiction",
"author": "Herman Melville",
"title": "Moby Dick",
"isbn": "0-553-21311-3",
"price": 8.99
},
{
"category": "fiction",
"author": "J. R. R. Tolkien",
"title": "The Lord of the Rings",
"isbn": "0-395-19395-8",
"price": 22.99
}
],
"bicycle": {
"color": "red",
"price": 19.95
}
},
"expensive": 10
}
Method to assert json:-
public void assertJsonvalue (String description, String jsonString,
String path, Object expectedValue) {
ObjectMapper objectMapper = new ObjectMapper();
Map<String, Object> jsonMap = null;
try {
jsonMap = objectMapper.readValue(jsonString,
new TypeReference<Map<String, Object>>() {
});
} catch (IOException e) {
fail("Could not parse json from string:" + jsonString);
}
Object actualValue = null;
try {
actualValue = PropertyUtils.getProperty(jsonMap, path);
System.out.println("actualValue" + actualValue);
} catch (IllegalAccessException e) {
// error here
}
assertEquals(description, expectedValue, actualValue);
}
When I try to get json value from by using the following, Its works well.
assertJsonValue("bicycle color", json, "store.bicycle.color", "red");
I want to get array value from json such as details of 1st book.
I have tried the following, that doesn't help me out.
json paths are as follows:-
"store.book[0]"
"store.book.[0]"
"store.book.category"
How Can I do this ?

According to this answer, you should be able to get the mentioned properties like this:
assertJsonValue("...", json, "store.(book)[0].category", "reference");
Edit:
Which version of jackson and beanutils are you using? I've modified your method a bit and made a simple test case, using beanutils 1.9.2 and jackson 2.6.5 tests seem to pass:
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.beanutils.PropertyUtils;
import org.junit.Test;
import java.io.IOException;
import static org.junit.Assert.*;
public class TestJson {
private static final String JSON = "{\n" +
" \"store\": {\n" +
" \"book\": [\n" +
" {\n" +
" \"category\": \"reference\",\n" +
" \"author\": \"Nigel Rees\",\n" +
" \"title\": \"Sayings of the Century\",\n" +
" \"price\": 8.95\n" +
" }\n" +
" ],\n" +
" \"bicycle\": {\n" +
" \"color\": \"red\",\n" +
" \"price\": 19.95\n" +
" }\n" +
" },\n" +
" \"expensive\": 10\n" +
"}";
#Test
public void testJson() {
assertTrue(assertJsonValue(JSON, "store.(book)[0].category", "reference"));
assertTrue(assertJsonValue(JSON, "store.(book)[0].author", "Nigel Rees"));
assertTrue(assertJsonValue(JSON, "store.(book)[0].title", "Sayings of the Century"));
assertTrue(assertJsonValue(JSON, "store.(book)[0].price", 8.95));
}
public boolean assertJsonValue(String jsonString,
String path,
Object expectedValue) {
ObjectMapper objectMapper = new ObjectMapper();
try {
Object actual = PropertyUtils
.getProperty(objectMapper.readValue(jsonString, Object.class), path);
if (actual.equals(expectedValue)) {
return true;
}
} catch (IOException | ReflectiveOperationException e) {
// handle error
}
return false;
}
}

Related

How to remove the "nameValuePairs" key from a json in java SpringBoot?

I have the following method that when receiving an xml in a String converts it to objects of type CitiMarketSSAEvent
public CitiMarketSSAEvent convertXmlToObject(String xml){
CitiMarketSSAEvent citiMarket = null;
JAXBContext jaxbContext = JAXBContext.newInstance(CitiMarketSSAEvent.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader(xml);
citiMarket = (CitiMarketSSAEvent) unmarshaller.unmarshal(reader);
return citiMarket;
}
and then I have the following method that converts the objects of that class into a json
public String convertObjectToJson(CitiMarketSSAEvent citiMarketObject) {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setPrettyPrinting();
gsonBuilder.disableHtmlEscaping();
Gson gson = gsonBuilder.create();
return gson.toJson(citiMarketObject, CitiMarketSSAEvent.class);
}
and it paints the json as follows
{
"header":{
"name": "transactionCount",
"version": "2.0",
"code": "1530",
"country": "MX",
"domain": "counts",
"time": "2018-10-11 11:20:34.323 GMT",
},
"body":{
"INPUNT": "I",
"MESSAGE_01": "RSTW",
"MESSAGE_02": "MNXTYP",
"MESSAGE_03": "RSTWERDCV",
"SEND_TIME": "20-NOV-2011 04:53:04 p.m.",
"RCV_ID": "ABGRCV",
"FORMAT0_MONTO": "200,000,300.00",
"FORMATO_MONEDA": "USD",
"CONTROL_01": "MSG1RCVSND",
"CONTROL_02": "MSG2RCVSND",
"CONTROL_03": "MSG3RCVSND",
}
}
but I want to add the "event" key to the json in such a way that I get something like this:
{
"event":{
"header":{
"name": "transactionCount",
"version": "2.0",
"code": "1530",
"country": "MX",
"domain": "counts",
"time": "2018-10-11 11:20:34.323 GMT",
},
"body":{
"INPUNT": "I",
"MESSAGE_01": "RSTW",
"MESSAGE_02": "MNXTYP",
"MESSAGE_03": "RSTWERDCV",
"SEND_TIME": "20-NOV-2011 04:53:04 p.m.",
"RCV_ID": "ABGRCV",
"FORMAT0_MONTO": "200,000,300.00",
"FORMATO_MONEDA": "USD",
"CONTROL_01": "MSG1RCVSND",
"CONTROL_02": "MSG2RCVSND",
"CONTROL_03": "MSG3RCVSND",
}
}
}
and then I modified my method as follows using JSONObject
public String convertObjectToJson(CitiMarketSSAEvent citiMarketObject) {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setPrettyPrinting();
gsonBuilder.disableHtmlEscaping();
JSONObject jsonObj = new JSONObject();
jsonObj.put("event",citiMarketObject);
Gson gson = gsonBuilder.create();
return gson.toJson(jsonObj, JSONObject.class);
}
and it throws me the json but the key "nameValuePairs" was added, how can it be removed? Or how else can I do it to just add the "event" key?
{
"nameValuePairs":{
"event":{
"header":{
"name": "transactionCount",
"version": "2.0",
"code": "1530",
"country": "MX",
"domain": "counts",
"time": "2018-10-11 11:20:34.323 GMT",
},
"body":{
"INPUNT": "I",
"MESSAGE_01": "RSTW",
"MESSAGE_02": "MNXTYP",
"MESSAGE_03": "RSTWERDCV",
"SEND_TIME": "20-NOV-2011 04:53:04 p.m.",
"RCV_ID": "ABGRCV",
"FORMAT0_MONTO": "200,000,300.00",
"FORMATO_MONEDA": "USD",
"CONTROL_01": "MSG1RCVSND",
"CONTROL_02": "MSG2RCVSND",
"CONTROL_03": "MSG3RCVSND",
}
}
}
}
Try this code, it is supposed to do what you need.
package com;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.LinkedHashMap;
public class Main {
public static void main(String[] args) throws JsonProcessingException {
String json = "{\n" +
" \"header\":{\n" +
" \"name\": \"transactionCount\",\n" +
" \"version\": \"2.0\",\n" +
" \"code\": \"1530\",\n" +
" \"country\": \"MX\",\n" +
" \"domain\": \"counts\",\n" +
" \"time\": \"2018-10-11 11:20:34.323 GMT\"\n" +
" },\n" +
" \"body\":{\n" +
" \"INPUNT\": \"I\",\n" +
" \"MESSAGE_01\": \"RSTW\",\n" +
" \"MESSAGE_02\": \"MNXTYP\",\n" +
" \"MESSAGE_03\": \"RSTWERDCV\",\n" +
" \"SEND_TIME\": \"20-NOV-2011 04:53:04 p.m.\",\n" +
" \"RCV_ID\": \"ABGRCV\",\n" +
" \"FORMAT0_MONTO\": \"200,000,300.00\",\n" +
" \"FORMATO_MONEDA\": \"USD\",\n" +
" \"CONTROL_01\": \"MSG1RCVSND\",\n" +
" \"CONTROL_02\": \"MSG2RCVSND\",\n" +
" \"CONTROL_03\": \"MSG3RCVSND\"\n" +
" }\n" +
"}";
ObjectMapper objectMapper = new ObjectMapper();
LinkedHashMap hashMap = objectMapper.readValue(json, LinkedHashMap.class);
Event event = new Event(hashMap);
System.out.println(objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(event));
}
public static class Event {
#JsonProperty("event")
private LinkedHashMap event;
public Event(LinkedHashMap event){
this.event = event;
}
}
}

How to map a complex JSON with POJO

I'm having some trouble with how to map a JSON correctly. I have to save this incoming JSON in my DB:
{
"request": {
"Target": "Affiliate_Offer",
"Format": "json",
"Service": "HasOffers",
"Version": "2",
"api_key": "3225235c56334584b1360d",
"Method": "findAll",
"contain": [
"Country"
]
},
"response": {
"status": 1,
"httpStatus": 200,
"data": {
"18292": {
"Offer": {
"id": "18292",
"name": "247 Profit Formula",
"approval_status": "approved"
},
"Country": {
"US": {
"id": "840",
"code": "US",
"name": "United States",
"regions": {
"4": {
"id": "4",
"country_code": "US",
"country_code_3c": "USA",
"code": "AR",
"name": "Arkansas"
},
"5": {
"id": "5",
"country_code": "US",
"country_code_3c": "USA",
"code": "CA",
"name": "California"
},
"10": {
"id": "10",
"country_code": "US",
"country_code_3c": "USA",
"code": "FL",
"name": "Florida"
}
},
"CA": {
"id": "124",
"code": "CA",
"name": "Canada",
"regions": []
}
}
},
"17823": {
"Offer": {
"id": "17823",
"name": "American Career Guide",
"approval_status": null
},
"Country": {
"Country": {
"US": {
"id": "840",
"code": "US",
"name": "United States",
"regions": []
},
"UK": {
"id": "826",
"code": "UK",
"name": "United Kingdom",
"regions": []
},
"AU": {
"id": "36",
"code": "AU",
"name": "Australia",
"regions": []
},
"CA": {
"id": "124",
"code": "CA",
"name": "Canada",
"regions": []
},
"NZ": {
"id": "554",
"code": "NZ",
"name": "New Zealand",
"regions": []
}
}
}
},
The Offer structure is already mapped, but I'm having some trouble with the Country.
The OfferMapper:
#Data
#AllArgsConstructor
#NoArgsConstructor
#JsonIgnoreProperties(ignoreUnknown = true)
public class OfferMapper {
OfferRequest request;
OfferResponse response;
}
The OfferResponse:
#Getter #Setter
#JsonIgnoreProperties(ignoreUnknown = true)
public class OfferResponse {
#JsonProperty("status")
private int status;
#JsonProperty("httpStatus")
private int httpStatus;
#JsonProperty("data")
private Map<String, OfferData> data;
}
Here is the structure that I'm having trouble. The OfferData:
#Getter #Setter
#JsonIgnoreProperties(ignoreUnknown = true)
public class OfferData {
#JsonProperty("Offer")
Offer offer;
#JsonProperty("Country")
Country country;
}
I tried to put in a map like this Map<Country, String> countries; and List<Map<Country, String>> countries; no luck so far to arrange this part with a POJO:
"Country": {
"US": {
"id": "840",
"code": "US",
"name": "United States",
"regions": []
}
}
"Country": {
"US": {
"id": "840",
"code": "US",
"name": "United States",
"regions": []
}
}
It is equivalent to receiving an object of Country that has an object of US.
Structure of US
public class US
{
private String id;
private String code;
private String name;
private List<String> regions; //if regions is gonna be list of strings
//getters and setters
}
Your Json Country is having letter C as capital hence it might be the issue that Jackson is not recognizing it. Check Java Naming conventions and other docs for more information.
class Data {
private String id;
private String code;
private String name;
private List<String> regions;
}
class CountryData {
public Map<String, Data> getCountry() {
return Country;
}
public void setCountry(Map<String, Data> Country) {
Country = Country;
}
//To resolve the error you need to add the below tag and define your class accordingly.
#JsonProperty("Country")
private Map<String, Data> Country;
}
public class POJO {
public static void main(String[] args) throws JsonProcessingException {
String countryjson = "{\n" +
" \"Country\": {\n" +
" \"US\": {\n" +
" \"id\": \"840\",\n" +
" \"code\": \"US\",\n" +
" \"name\": \"United States\",\n" +
" \"regions\": []\n" +
" },\n" +
" \"UK\": {\n" +
" \"id\": \"826\",\n" +
" \"code\": \"UK\",\n" +
" \"name\": \"United Kingdom\",\n" +
" \"regions\": []\n" +
" },\n" +
" \"AU\": {\n" +
" \"id\": \"36\",\n" +
" \"code\": \"AU\",\n" +
" \"name\": \"Australia\",\n" +
" \"regions\": []\n" +
" },\n" +
" \"CA\": {\n" +
" \"id\": \"124\",\n" +
" \"code\": \"CA\",\n" +
" \"name\": \"Canada\",\n" +
" \"regions\": []\n" +
" },\n" +
" \"NZ\": {\n" +
" \"id\": \"554\",\n" +
" \"code\": \"NZ\",\n" +
" \"name\": \"New Zealand\",\n" +
" \"regions\": []\n" +
" }\n" +
" }\n" +
"}";
ObjectMapper objectMapper = new ObjectMapper();
CountryData country = objectMapper.readValue(countryjson, CountryData.class);
System.out.println(country.toString());
}
}
It should work, Add #JsonProperty Tag in you POJO definition.

Obtaining value from Json using Gson get()

Given the following Json data:
"values": [
{
"type": "any",
"value": "MO918038B",
"key": "nino"
},
{
"type": "any",
"value": "1956-11-18",
"key": "dob"
},
{
"type": "any",
"value": "q",
"key": "memorableWord"
},
{
"type": "any",
"value": "E13468",
"key": "pin"
},
]
And the following Java class:
public void doStuff() {
try (Reader reader = new FileReader("global-vars.json")) {
JsonObject json = new Gson().fromJson(reader, JsonObject.class);
String reg = json.get("values").toString();
System.out.println(reg);
} catch (IOException e) {
e.printStackTrace();
}
}
I'm not clear, after searching, how best to obtain the "value" of, for example, "nino" which is = "MO918038B"
Type listType = new TypeToken<List<YourObjectClass>>() {}.getType();
List<YourObjectClass> yourList = new Gson().fromJson(yourJson, listType);
Since your posted JSON isn't RFC 4627 conformant, I assume this array is inside of an object.
Code would look like this:
public class YourClass {
class Value {
public String type;
public String value;
public String key;
}
public Value[] values;
}
public class App{
public static void main(String[] args) {
String json = "{ \"values\": [\n" +
"{\n" +
" \"type\": \"any\",\n" +
" \"value\": \"MO918038B\",\n" +
" \"key\": \"nino\"\n" +
"},\n" +
"{\n" +
" \"type\": \"any\",\n" +
" \"value\": \"1956-11-18\",\n" +
" \"key\": \"dob\"\n" +
"},\n" +
"{\n" +
" \"type\": \"any\",\n" +
" \"value\": \"q\",\n" +
" \"key\": \"memorableWord\"\n" +
"},\n" +
"{\n" +
" \"type\": \"any\",\n" +
" \"value\": \"E13468\",\n" +
" \"key\": \"pin\"\n" +
"}\n" +
"]}";
YourClass yourClass = new Gson().fromJson(json, YourClass.class);
for (YourClass.Value value : yourClass.values) {
if (value.key.equals("nino")) {
System.out.println(value.value);
}
}
}
}

How to converting a nested Json object/array to multiple lists based on keys Dynamically without knowing the keys

The below is a sample Json file.
{"Yjson":
[
{
"Name": "crunchify.com",
"Author": "App Shah",
"Address": "New York",
"Company Services": [{
"Service": "Site SEO Review",
"Link": "https://crunchify.com/services/site-seo-review-service/"
}, {
"Service": "Full Website Design Service",
"Link": "https://crunchify.com/services/full-website-design-service/"
}, {
"Service": "WordPress Optimization & Consultation",
"Link": "https://crunchify.com/services/wordpress-optimization-service/"
}, {
"Service": "WordPress Optimization & Consultation",
"Link": "https://crunchify.com/services/wordpress-optimization-service/"
}]
},
{
"Name": "xyz.com",
"Author": "xyz Shah",
"Address": "toronto",
"Company Services": [{
"Service": "Site SEO Review",
"Link": "https://crunchify.com/services/site-seo-review-service/"
}, {
"Service": "Full Website Design Service",
"Link": "https://crunchify.com/services/full-website-design-service/"
}, {
"Service": "WordPress Optimization & Consultation",
"Link": "https://crunchify.com/services/wordpress-optimization-service/"
}]
}
]
}
How to store all the values of each key in a Arraylist in java ?
such as the array list for key name will contain [crunchicy.com,xyz.com]. similarly for every key there should be a array list.
example :
list name : [crunchify.com,xyz]
list Author : [aap shah,xyz shah]
list name : [newyork,toronto]
How to parse every json object that can be nested and dynamic in
nature and keep them in array lists according to keys ??
Here is code for reading your JSON text using Jackson Databind and writing it to CSV using Apache Commons CSV.
When using Databind, you need Java POJO classes, optionally annotated using #JsonProperty to specify the JSON field names.
Your JSON text is included inline at the bottom to produce an MCVE.
import java.util.List;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVPrinter;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Test {
public static void main(String[] args) throws Exception {
Root root = new ObjectMapper().readValue(Input.json, Root.class);
CSVPrinter printer = CSVFormat.DEFAULT
.withHeader("Name", "Author", "Address", "Service", "Link")
.print(System.out);
for (Site site : root.getSites())
for (Service service : site.getServices())
printer.printRecord(site.getName(), site.getAuthor(), site.getAddress(),
service.getService(), service.getLink());
}
}
class Root {
private List<Site> sites;
#JsonProperty("Yjson")
public List<Site> getSites() {
return this.sites;
}
public void setSites(List<Site> sites) {
this.sites = sites;
}
}
class Site {
private String name;
private String author;
private String address;
private List<Service> services;
#JsonProperty("Name")
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
#JsonProperty("Author")
public String getAuthor() {
return this.author;
}
public void setAuthor(String author) {
this.author = author;
}
#JsonProperty("Address")
public String getAddress() {
return this.address;
}
public void setAddress(String address) {
this.address = address;
}
#JsonProperty("Company Services")
public List<Service> getServices() {
return this.services;
}
public void setServices(List<Service> services) {
this.services = services;
}
}
class Service {
private String service;
private String link;
#JsonProperty("Service")
public String getService() {
return this.service;
}
public void setService(String service) {
this.service = service;
}
#JsonProperty("Link")
public String getLink() {
return this.link;
}
public void setLink(String link) {
this.link = link;
}
}
class Input {
static final String json =
"{\"Yjson\":\n" +
"[\n" +
"{\n" +
"\"Name\": \"crunchify.com\",\n" +
" \"Author\": \"App Shah\",\n" +
" \"Address\": \"New York\",\n" +
" \"Company Services\": [{\n" +
" \"Service\": \"Site SEO Review\",\n" +
" \"Link\": \"https://crunchify.com/services/site-seo-review-service/\"\n" +
" }, {\n" +
" \"Service\": \"Full Website Design Service\",\n" +
" \"Link\": \"https://crunchify.com/services/full-website-design-service/\"\n" +
" }, {\n" +
" \"Service\": \"WordPress Optimization & Consultation\",\n" +
" \"Link\": \"https://crunchify.com/services/wordpress-optimization-service/\"\n" +
" }, {\n" +
" \"Service\": \"WordPress Optimization & Consultation\",\n" +
" \"Link\": \"https://crunchify.com/services/wordpress-optimization-service/\"\n" +
" }]\n" +
"},\n" +
"{\n" +
" \"Name\": \"xyz.com\",\n" +
" \"Author\": \"xyz Shah\",\n" +
" \"Address\": \"toronto\",\n" +
" \"Company Services\": [{\n" +
" \"Service\": \"Site SEO Review\",\n" +
" \"Link\": \"https://crunchify.com/services/site-seo-review-service/\"\n" +
" }, {\n" +
" \"Service\": \"Full Website Design Service\",\n" +
" \"Link\": \"https://crunchify.com/services/full-website-design-service/\"\n" +
" }, {\n" +
" \"Service\": \"WordPress Optimization & Consultation\",\n" +
" \"Link\": \"https://crunchify.com/services/wordpress-optimization-service/\"\n" +
" }]\n" +
"}\n" +
"]\n" +
"}";
}
Output
Name,Author,Address,Service,Link
crunchify.com,App Shah,New York,Site SEO Review,https://crunchify.com/services/site-seo-review-service/
crunchify.com,App Shah,New York,Full Website Design Service,https://crunchify.com/services/full-website-design-service/
crunchify.com,App Shah,New York,WordPress Optimization & Consultation,https://crunchify.com/services/wordpress-optimization-service/
crunchify.com,App Shah,New York,WordPress Optimization & Consultation,https://crunchify.com/services/wordpress-optimization-service/
xyz.com,xyz Shah,toronto,Site SEO Review,https://crunchify.com/services/site-seo-review-service/
xyz.com,xyz Shah,toronto,Full Website Design Service,https://crunchify.com/services/full-website-design-service/
xyz.com,xyz Shah,toronto,WordPress Optimization & Consultation,https://crunchify.com/services/wordpress-optimization-service/
If you use Maven, these are the two dependencies you need:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
<version>1.4</version>
</dependency>

GSON parsing fromJSON regarding arrays of objects exception

I am using google's GSON parsing library and I'm not sure if this has been asked before (I did check what I could find) but, here I go!! It's regarding the fromJSON method and how it refuses to interpret some classes as arrays.
Given the JSON structure below:
{"visualization": {
"root": {
"fullname": "CC/dudu",
"name": "dudu",
"type": "String",
"children": [
{
"fullname": "CC/dudu/lulu",
"name": "lulu",
"type": "String"
}
]
},
"traces": {
"messages": [
{
"from": "dudu",
"method": "call()",
"scenario": "#1",
"timestamp": "09-12-2013 00:21:14",
"to": "dudu",
"type": "void",
"violation": "true",
"visible": "true"
}
],
"scenarios": [
{
"name": "testscenario",
"id": "#1",
"description": "testing parsing!"
}
]
}
}}
And the accompanying contained classes.
public class Response {
private Visualization visualization;
//+getter/setter
}
public class Visualization {
private Component root;
private Map<String, Trace> traces;
//+getter/setter
}
public class Trace {
private ArrayList<Message> messages;
private ArrayList<Scenario> scenarios;
//+getter/setter
}
I get the error that GSON was expecting an Object and NOT an Array (before the "[" token of messages). Anyone know why that is? The types (as can be seen in the classes) are List, so it should be fine. And I have tried having more objects in the array and I still get the same error message! Why is Gson interpreting the List<TypeA> type as an object and not an array?
EDIT:
Here's the code code, but it's kinda pointless, since the exception is being thrown because of the parsing process. I doubt you'll find anything useful. "visualization" is a string with a correct JSON format.
Gson gsonParser = new Gson();
Response r = gsonParser.fromJson(visualization, Response.class);
The code below allows you to parse exactly your JSON.
Note that I put all into a class to make easier for you to test it. Anycase, Gson does not work well with inner classes unless they are static. So I suggest you to make a file for each class or use only static inner classes.
package stackoverflow.questions;
import java.util.*;
import com.google.gson.Gson;
public class Q20461706 {
public class Trace {
ArrayList<Message> messages;
ArrayList<Scenario> scenarios;
#Override
public String toString() {
return "Trace [messages=" + messages + ", scenarios=" + scenarios + "]";
}
}
public static class Message {
String from; // "from": "dudu",
String method; // "method": "call()",
String scenario; // "scenario": "#1",
String timestamp; // "timestamp": "09-12-2013 00:21:14",
String to; // "to": "dudu",
String type; // "type": "void",
Boolean violation; // "violation": "true",
Boolean visible; // "visible": "true"
#Override
public String toString() {
return "Message [from=" + from + ", method=" + method + ", scenario=" + scenario + ", timestamp=" + timestamp + ", to=" + to + ", type=" + type + ", violation=" + violation + ", visible=" + visible + "]";
}
}
public static class Scenario {
String name;// "name": "testscenario",
String id; // "id": "#1",
String description; // "description": "testing parsing!"
#Override
public String toString() {
return "Scenario [name=" + name + ", id=" + id + ", description=" + description + "]";
}
}
public static class Component {
String fullname; // "fullname": "CC/dudu",
String name; // "name": "dudu",
String type; // "type": "String",
List<Component> children;
#Override
public String toString() {
return "Component [fullname=" + fullname + ", name=" + name + ", type=" + type + ", children=" + children + "]";
}
}
public static class Visualization {
Component root;
Trace traces;
#Override
public String toString() {
return "Visualization [root=" + root + ", traces=" + traces + "]";
}
}
public static class Response {
Visualization visualization;
#Override
public String toString() {
return "Response [visualization=" + visualization + "]";
}
}
public static void main(String[] args) {
String json =
" {\"visualization\": { "+
" \"root\": { "+
" \"fullname\": \"CC/dudu\", "+
" \"name\": \"dudu\", "+
" \"type\": \"String\", "+
" \"children\": [ "+
" { "+
" \"fullname\": \"CC/dudu/lulu\", "+
" \"name\": \"lulu\", "+
" \"type\": \"String\" "+
" } "+
" ] "+
" }, "+
" \"traces\": { "+
" \"messages\": [ "+
" { "+
" \"from\": \"dudu\", "+
" \"method\": \"call()\", "+
" \"scenario\": \"#1\", "+
" \"timestamp\": \"09-12-2013 00:21:14\", "+
" \"to\": \"dudu\", "+
" \"type\": \"void\", "+
" \"violation\": \"true\", "+
" \"visible\": \"true\" "+
" } "+
" ], "+
" \"scenarios\": [ "+
" { "+
" \"name\": \"testscenario\", "+
" \"id\": \"#1\", "+
" \"description\": \"testing parsing!\" "+
" } "+
" ] "+
" } "+
" }} ";
Gson gsonParser = new Gson();
Response r = gsonParser.fromJson(json, Response.class);
System.out.println(r);
}
}
To respond to your question, note that I changed your Visualization class with Trace traces since you have only a Trace object in your JSON and not an array. This is why Gson complains.
Note also that I avoided to parse the date as date, you need to specify a custom date format for that, but it's beyond the scope of this question. You can find many examples here on SO.

Categories

Resources