Convert List<MyDTO> to List<Map<String,String>> using Gson - java

I have DTO as below :
public class MyDTO {
private String MEValueName;
private String MyId;
// getters and setters
}
Gson gson = new Gson();
List<MyDTO> list = new ArrayList<MyDTO>();
MyDTO dto = new MyDTO();
dto.setMEValueName("raghu");
dto.setMyId("qwer");
MyDTO dto1 = new MyDTO();
dto1.setMEValueName("raghuveer");
dto1.setMyId("qwer1");
list.add(dto);
list.add(dto1);
String json = gson.toJson(list);
System.out.println(json);
// below line is failing
List<Map<String, String>> list1 = gson.fromJson(json, new TypeToken<List<Map<String, String>>>(){}.getType());
System.out.println(list1);
when i run this i get the follow error :
java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 11 path $[0].
Also i want to use a generic type so that i can use other DTOs also to convert to list of map. Kindly suggest.

I see two immediate problems...
The json-string is of an object of type List<MyDTO>.
gson.fromJson must have a TypeToken of that same object type.
And even if the fromJson worked, you cant assign List<MyDTO> to a list of type List<Map<String,String>>.
Edit (doing this by memory so it might need some fine tuning to compile):
List<Map<String, String>> list1 = new List();
Map<String,String> map = new HashMap<>();
list1.add(map);
map.add("some key",json);

Related

Jackson deserialize json string with "pseudo array" of objects

I'm trying to read data from this API https://coinmarketcap.com/api/
For this endpoint https://api.coinmarketcap.com/v2/ticker/.
I'm having issues mapping the data field to a POJO. The field really contains an array of objects but in terms of the json it's not really defined as an array.
i.e. instead of
data: [{"id":"1","name":"some object"},{"id":"5","name":"another object"},...]
the json has named fields like so
data: {"1":{"id":"1","name":"some object"},"5":{"id":"5","name":"another object"},...}
I can manually parse this using
objectMapper.readTree(new URL("https://api.coinmarketcap.com/v2/ticker/"));
but is there a way automatically map these to a List?
You can parse it into a map (as #teppic said) and then get the map values as a list.
To deserialize into a map, you can see the answer from this question: Deserializing into a HashMap of custom objects with jackson
TypeFactory typeFactory = mapper.getTypeFactory();
MapType mapType = typeFactory.constructMapType(HashMap.class, String.class, Theme.class);
HashMap<String, Theme> map = mapper.readValue(json, mapType);
Assuming you have a class called Item with the id and name fields, you can do this:
String json = "{\"1\":{\"id\":\"1\",\"name\":\"some object\"},\"5\":{\"id\":\"2\",\"name\":\"another object\"}}";
ObjectMapper mapper = new ObjectMapper();
// create your map type <String, Item>
TypeFactory typeFactory = mapper.getTypeFactory();
MapType mapType = typeFactory.constructMapType(HashMap.class, String.class, Item.class);
HashMap<String, Item> map = mapper.readValue(json, mapType);
// get the list
List<Item> list = new ArrayList<Item>(map.values());
System.out.println(list);
Output:
[Item [id=1, name=some object], Item [id=2, name=another object]]
Your other option would be a custom deserializer, or reading the tree as you mentioned.
try this
String json = "[{\"name\":\"Steve\",\"lastname\":\"Jobs\"}]";
JsonArray jArray = (JsonArray)new JsonParser().parse(json);
String sName = jArray.get(0).getAsJsonObject().get("name").getAsString());
String sLastName = jArray.get(0).getAsJsonObject().get("lastname").getAsString());
see you later.

remove backslash from display of string(gson)

I have the list
Gson gson = new Gson();
List<String> exampleList = new ArrayList<String>();
exampleList.add("aaa");
exampleList.add("bbb");
exampleList.add("ccc");
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("test", gson.toJson(exampleList));
And jsonObject is {"test":"[\"aaa\",\"bbb\",\"ccc\"]"}
but i need get following
{"test":["aaa","bbb","ccc"]}
What the way to do this?
replaceAll in several ways is not solving this problem
You're adding a key-value mapping String -> String, that is why the quotes are escaped (in fact your value is the string representation of the list given by the toString() method). If you want a mapping String -> Array, you need to convert the list as a JsonArray and add it as a property.
jsonObject.add("test", gson.toJsonTree(exampleList, new TypeToken<List<String>>(){}.getType()));
Don't mix Gson and JsonObject,
1) if you need {"test":["aaa","bbb","ccc"]} using GSON you should define
public class MyJsonContainer {
List<String> test = new ArrayList<String>();
...
// getter and setter
}
and use
List<String> exampleList = new ArrayList<String>();
exampleList.add("aaa");
exampleList.add("bbb");
exampleList.add("ccc");
MyJsonContainer jsonContainer = new MyJsonContainer();
jsonContainer.setTest(exampleList);
String json = gson.toJson(jsonContainer); // this json has {"test":["aaa","bbb","ccc"]}
2) if you need {"test":["aaa","bbb","ccc"]} using JsonObject you should just add
List<String> exampleList = new ArrayList<String>();
exampleList.add("aaa");
exampleList.add("bbb");
exampleList.add("ccc");
JsonObject jsonObject = new JsonObject();
jsonObject.addProperty("test", exampleList);
But never try to mix Gson and JsonObject, because jsonObject.addProperty("test", text) does not allowed to add text as json and allways escaped this text.
String jsonFormattedString = jsonStr.replaceAll("\\\\", "");
Use this for remove \ from string of object.

How do I get differences between two json objects using GSON?

I used this code to compare two JSON object using Gson in Android:
String json1 = "{\"name\": \"ABC\", \"city\": \"XYZ\"}";
String json2 = "{\"city\": \"XYZ\", \"name\": \"ABC\"}";
JsonParser parser = new JsonParser();
JsonElement t1 = parser.parse(json1);
JsonElement t2 = parser.parse(json2);
boolean match = t2.equals(t1);
Is there any way two get the differences between two objects using Gson in a JSON format?
If you deserialize the objects as a Map<String, Object>, you can with Guava also, you can use Maps.difference to compare the two resulting maps.
Note that if you care about the order of the elements, Json doesn't preserve order on the fields of Objects, so this method won't show those comparisons.
Here's the way you do it:
public static void main(String[] args) {
String json1 = "{\"name\":\"ABC\", \"city\":\"XYZ\", \"state\":\"CA\"}";
String json2 = "{\"city\":\"XYZ\", \"street\":\"123 anyplace\", \"name\":\"ABC\"}";
Gson g = new Gson();
Type mapType = new TypeToken<Map<String, Object>>(){}.getType();
Map<String, Object> firstMap = g.fromJson(json1, mapType);
Map<String, Object> secondMap = g.fromJson(json2, mapType);
System.out.println(Maps.difference(firstMap, secondMap));
}
This program outputs:
not equal: only on left={state=CA}: only on right={street=123 anyplace}
Read more here about what information the resulting MapDifference object contains.

Java extract strings/tags?

I have sets of strings that look like this:
[{"tag":"rat","score":0.7973},{"tag":"lion","score":0.7184},{"tag":"dog","score":0.2396},{"tag":"woof","score":0.1944},{"tag":"cat","score":0.1157}]
I would like to print the following in order from the string:
rat
lion
dog
woof
cat
How can i do this?
Proper use any JSON parsing library such as GSON or Jackson and convert it into Java Object.
Note: It returns java.util.LinkedHashMap that maintains the order.
Sample code:
GSON
String jsonString="[{\"tag\":\"rat\",\"score\":0.7973},{\"tag\":\"lion\",\"score\":0.7184},{\"tag\":\"dog\",\"score\":0.2396},{\"tag\":\"woof\",\"score\":0.1944},{\"tag\":\"cat\",\"score\":0.1157}]";
Type type = new TypeToken<ArrayList<Map<String, String>>>() {}.getType();
ArrayList<Map<String, String>> data = new Gson().fromJson(jsonString, type);
for (Map<String, String> map : data) {
System.out.println(map.get("tag"));
}
Jackson
String jsonString="[{\"tag\":\"rat\",\"score\":0.7973},{\"tag\":\"lion\",\"score\":0.7184},{\"tag\":\"dog\",\"score\":0.2396},{\"tag\":\"woof\",\"score\":0.1944},{\"tag\":\"cat\",\"score\":0.1157}]";
TypeReference<ArrayList<Map<String, String>>> typeRef = new TypeReference<ArrayList<Map<String, String>>>() {};
ObjectMapper mapper = new ObjectMapper();
try {
ArrayList<Map<String, String>> data = mapper.readValue(jsonString, typeRef);
for (Map<String, String> map : data) {
System.out.println(map.get("tag"));
}
} catch (Exception e) {
System.out.println("There might be some issue with the JSON string");
}
output:
rat
lion
dog
woof
cat
(?:.*?{"tag":)"(.*?)",.*?}
This will match all the required groups.See Demo
http://regex101.com/r/aN1bX5/1
This string is JSON so you could use a JSON parser to do this.
If you're looking for an easy way to get all the values of "tag" use this regex and extract group 1 in Java.
"tag":"((?:[^"\\]|\\.)*)"
Debuggex Demo
Double-escape this if you're gonna use it in Java:
Pattern p = Pattern.compile("\"tag\":\"((?:[^\"\\\\]|\\\\.)*)\"");
Try this. The match is in the first capture group
(?:"tag":"|\G(?!^))([^"]*)
Dont use regex to parse JSON. Use a library like GSON or Jackson
JSONArray jsa = new JSONArray(jsonString);
for (int i = 0; i < jsa.length(); i++) {
JSONObject json = jsa.getJSONObject(i);
System.out.println(json.getString("tag"));
}

Get JSON key name using GSON

I have a JSON array which contains objects such as this:
{
"bjones": {
"fname": "Betty",
"lname": "Jones",
"password": "ababab",
"level": "manager"
}
}
my User class has a username which would require the JSON object's key to be used. How would I get the key of my JSON object?
What I have now is getting everything and creating a new User object, but leaving the username null. Which is understandable because my JSON object does not contain a key/value pair for "username":"value".
Gson gson = new Gson();
JsonParser p = new JsonParser();
JsonReader file = new JsonReader(new FileReader(this.filename));
JsonObject result = p.parse(file).getAsJsonObject().getAsJsonObject("bjones");
User newUser = gson.fromJson(result, User.class);
// newUser.username = null
// newUser.fname = "Betty"
// newUser.lname = "Jones"
// newUser.password = "ababab"
// newUser.level = "manager"
edit:
I'm trying to insert "bjones" into newUser.username with Gson, sorry for the lack of clarification
Use entrySet to get the keys. Loop through the entries and create a User for every key.
JsonObject result = p.parse(file).getAsJsonObject();
Set<Map.Entry<String, JsonElement>> entrySet = result.entrySet();
for(Map.Entry<String, JsonElement> entry : entrySet) {
User newUser = gson.fromJson(p.getAsJsonObject(entry.getKey()), User.class);
newUser.username = entry.getKey();
//code...
}
Using keySet() directly excludes the necessity in iteration:
ArrayList<String> objectKeys =
new ArrayList<String>(
myJsonObject.keySet());
Your JSON is fairly simple, so even the manual sort of methods (like creating maps of strings etc for type) will work fine.
For complex JSONs(where there are many nested complex objects and lists of other complex objects inside your JSON), you can create POJO for your JSON with some tool like http://www.jsonschema2pojo.org/
And then just :
final Gson gson = new Gson();
final MyJsonModel obj = gson.fromJson(response, MyJsonModel.class);
// Just access your stuff in object. Example
System.out.println(obj.getResponse().getResults().get(0).getId());

Categories