hashmap to JSON using GSON - java

I am using Gson for converting my Java Objects to GSON. I wanted to convert my HashMap to JSON.
Assuming my Object1 has structure:
public class Object1 {
String a;
String b;
String c;
String toString() {
return "a: " + a + " b: " + b + " c: " + c;
}
}
and my Object2 has Structure:
public class Object2 {
String e;
String f;
}
I wanted my final JSON to look like
{
{
"a" : "aValue",
"b" : "bValue",
"c" : "cValue"
} : {
"e" : "eValue",
"f" : "fValue"
}
}
Bit I am getting it as
{
"a: aValue b: bValue c: cValue" : {
"e" : "eValue",
"f" : "fValue"
}
}
Is this possible to have it in desired form.
I tried using TypeToken given in JSON documentation. That didn't help.

You cannot get your expected form.
JSON's element is always a key-value paar and the key is always a text (or String). But in your case the key is an object.
From my understanding, if you wanna to consume the key as an Object, you could get it as a String and then use ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper) to convert it to your expected class.
final ObjectMapper mapper = new ObjectMapper();
Object1 object1 = mapper.readValue(object1String, Object1.class)

Your toString() method may be causing issues, Gson will process the map itself, there is no need to provide a toString() like you have. That's why your output is giving you that.
Also take a look at Jackson, it would be fit for your purpose and is very easy to use.
Map map = new HashMap();
ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(map);
// write json to file

You can write your toString method as:
public String toString() {
return "{a: " + a + ", b: " + b + ", c: " + c + "}";
}
In your main class, you call:
public static void main(String[] args) {
Object1 object1 = new Object1("aValue", "bValue", "cValue");
Object2 object2 = new Object2("eValue", "fValue");
Map<String, Object2> map = new HashMap<String, Object2>();
map.put(object1.toString(), object2);
String json = new Gson().toJson(map);
}
Then, your output like: {"{a: aValue, b: bValue, c: cValue}":{"e":"eValue","f":"fValue"}}

Related

parse strange Json response to a list

I want to parse this object to a list of string. I do not need the key but just want the value as a list of string.
I cannot have a simple model classes because the keys object are more than 1000 in some responses and are random.
So please any idea how to parse it to list in kotlin or java?
{
"data": {
"21": "593754434425",
"22": "4560864343802",
"23": "7557134347529",
"24": "5937544344255",
"25": "45608643438024",
"26": "75571343475293"
}
}
You could first deserialize it as it is, and then convert to a list.
The JSON can be represented this way:
data class Response(val data: Map<String, String>)
You can mark this class #Serializable and use Kotlinx Serialization to deserialize it, or you can use other libraries like Moshi or Jackson (with jackson-module-kotlin).
Once it's deserialized, simply get the values of the map (it's a collection):
val response = Json.decodeFromString<Response>(yourJsonString)
// this is a Collection, not List, but it should be good enough
val stringValues = response.data.values
// if you really need a List<String>
val list = stringValues.toList()
If you want to get the values in the natural order of the keys, you can also use something like:
val values = response.data.toSortedMap(compareBy<String> { it.toInt() }).values
You can use this to parse your data:
val it: Iterator<String> = json.keys()
val arrayList = ArrayList<String>()
while (it.hasNext()) {
val key = it.next()
arrayList.add(json.get(key))
}
A better way is to change the json model, if you access it.
{
"data": [
"593754434425","4560864343802",
"7557134347529","5937544344255",
"45608643438024","75571343475293"
]
}
For this problem, its handy to use the libriary org.json.
See following code snippet:
import org.json.JSONObject;
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
// Defining the input
String input = "{\n" +
" \"data\": {\n" +
" \"21\": \"593754434425\",\n" +
" \"22\": \"4560864343802\",\n" +
" \"23\": \"7557134347529\",\n" +
" \"24\": \"5937544344255\",\n" +
" \"25\": \"45608643438024\",\n" +
" \"26\": \"75571343475293\"\n" +
" }\n" +
"}\n";
// Parsing it to a json object with org.json
JSONObject inputJson = new JSONObject(input);
// If inputJson does not contain the key data, we return
if(!inputJson.has("data")) return;
// Else we read this data object to a new JSONObject
JSONObject dataJson = inputJson.getJSONObject("data");
// Define an array list where all the values will be contained
ArrayList<String> values = new ArrayList<>();
// Get a key set of the dat json object. For each key we get its respective value and add it to our value array list
for (String key : dataJson.keySet()) values.add(dataJson.getString(key));
// Print all values
for (String value : values) System.out.println(value);
}
}
=>
4560864343802
7557134347529
5937544344255
45608643438024
75571343475293
593754434425
Installing org.json is the easiest with a package manager like maven or gradle.
Guys i have comeup with a similar solution for the problem here
this is my model class
data class UnVerifiedTagIds(
#SerializedName("data")
val data: Object
)
and this is how i parse the respone here
val values: ArrayList<String> = ArrayList()
val list_of_tag_ids: ArrayList<String> =response.data as ArrayList<String>
The ist one is the dataclass for the response
and the 2nd one is the ApiCallInterface m using Retrofit...
and the last one is the apicall itself
I am using Kotlin language
do class name with name like this data class Result(val data:Map<String,String>)
and using library GSON for convert string json to this model
val json = "{\n" +
" \"data\": {\n" +
" \"21\": \"593754434425\",\n" +
" \"22\": \"4560864343802\",\n" +
" \"23\": \"7557134347529\",\n" +
" \"24\": \"5937544344255\",\n" +
" \"25\": \"45608643438024\",\n" +
" \"26\": \"75571343475293\"\n" +
" }\n" +
"}"
val dat = Gson().fromJson(json,Result::class.java)
if (dat.data.isNotEmpty()){
val list= dat.data.values.toMutableList()
print(list)
}
that works fine with me

How To Return First And Last Element Of Json

Using gson how can I return the first and last element from my json so I get the data in this format?
System.out.println("Student: BobGoblin - result: 59");
I have tried this, but it still returns the full JSON object
JsonObject jsonObject = new Gson().fromJson(content.toString(), JsonObject.class);
return jsonObject.get(domain) + " - " + jsonObject.get(result.toString());
First of all: toJson converts something to json. You want to convert json to some kind of object. So use fromJson instead.
Second build an object where you can put that data into. There are plenty examples on the manual site for gson: https://github.com/google/gson/blob/master/UserGuide.md
Let me code that for you. It's not that hard:
import java.util.Map;
import com.google.gson.Gson;
public class GsonTest {
public static void main(String args[]) {
Gson gson = new Gson();
String json = "{\"name\":\"Bog\", \"foo\":\"bar\", \"result\": 59}";
// Using a map
#SuppressWarnings( "unchecked" )
Map<String,Object> map = gson.fromJson( json, Map.class );
System.out.println( "Name: " + map.get( "name" ) + " result: " + map.get( "result" ) );
// Better: using an object
Student student = gson.fromJson( json, Student.class );
System.out.println( "Name: " + student.name + " result: " + student.result );
}
public static class Student {
public String name;
public String foo;
public int result;
}
}
which will result in:
Name: Bog result: 59.0
Name: Bog result: 59
The general Method is: Take the json String and put it in some kind of java object. Then access that java object to get to your data.
Note that you get more control over the kind of data you will receive using the second method. Since json doesn't specify the datatype the parser guesses float/double for age while it uses int in the second example because the class said so.

Get nested JSON elements from ObjectNode using Jackson

I have an ObjectNode, that looks as follows
{
"Header":{
"sub-header1":{
"#field":"value",
"#field":"value",
},
"sub-header2":{
"field":"",
"field":"",
"field":"",
"panel_field":{
"value":"",
"value":""
}
}
}
Now, what I want to do is to get all the fields from sub-headers in a list. This is the method that I'm employing
public static List<String> getDocumentFields(ObjectNode jsonDocumentNode) {
List<String> documentFields = new ArrayList<>();
Iterator<JsonNode> fields = jsonDocumentNode.elements();
while (fields.hasNext()) {
JsonNode jsonNode = fields.next();
Iterator<Map.Entry<String, JsonNode>> jsonFields = jsonNode.fields();
while (jsonFields.hasNext()) {
Map.Entry<String, JsonNode> jsonNodeEntry = jsonFields.next();
documentFields.add(jsonNodeEntry.getKey());
}
}
return documentFields;
}
But, I'm only getting headers in the list like {sub-header1, sub-header2}, instead of fields. How can I fix this? I'd really appreciate any kind of help.
EDIT:
While #sfiss's answer helped a great deal, I still wanted to find a way to do so without hardcoding the loop-logic and this answer turned out to be the exact thing I was looking for.
Well, it's simple, you are not iterating deep enough (field list is on third level). If you know the structure of your JSON, just iterate until you find the desired fields:
public class MyTest {
#Test
public void testJson() throws IOException {
final String json = getJson();
final JsonNode jsonDocumentNode = new ObjectMapper().readTree(json);
final List<String> fields = getDocumentFields((ObjectNode) jsonDocumentNode);
assertThat(fields, Matchers.contains("#field1", "#field2", "field1", "field2", "field3", "panel_field"));
}
public static String getJson() {
return "{\r\n" +
" \"Header\":{\r\n" +
" \"sub-header1\":{\r\n" +
" \"#field1\":\"value\",\r\n" +
" \"#field2\":\"value\"\r\n" +
" },\r\n" +
" \"sub-header2\":{\r\n" +
" \"field1\":\"\",\r\n" +
" \"field2\":\"\",\r\n" +
" \"field3\":\"\",\r\n" +
" \"panel_field\":{\r\n" +
" \"value1\":\"\",\r\n" +
" \"value2\":\"\"\r\n" +
" }\r\n" +
" } \r\n" +
" }\r\n" +
"}";
}
public static List<String> getDocumentFields(final ObjectNode jsonDocumentNode) {
final List<String> documentFields = new ArrayList<>();
for (final JsonNode header : (Iterable<JsonNode>) jsonDocumentNode::elements) {
for (final JsonNode subheader : (Iterable<JsonNode>) header::elements) {
for (final Map.Entry<String, JsonNode> field : (Iterable<Map.Entry<String, JsonNode>>) subheader::fields) {
documentFields.add(field.getKey());
}
}
}
return documentFields;
}
}
However, I would argue that it is simpler to let jackson serialize the JSON into a convenient data structure, and you just use the POJO's getters to obtain your values. That would also make it more clear than handling the JsonNode.
Just FYI, I edited your JSON slightly and used Java 8 SAM conversions to create iterables for the foreach-loops from the iterators, but you can still use your code and iterate one more level using while and iterators.

POJO with array of integers to deserialize

I am having some trouble deserializing the following JSON into a POJO. I have no control over the JSON structure, else I would've implemented it in some other way, but, that's life for you.
{
"1":{
"test":"1",
"other":"stuff"
},
"2":{
"test":"2",
"other":"stuff2"
}
}
Anyway, I am trying to deserialize by using a POJO with:
public Map<Integer, Payload> payload;
but although the Map does have a size of 2, when I try to get each of it, it's contents are null. Any idea on what I am doing wrong?
Thank you
I have no idea how the payload class looks like, but it should be something like this:
class Payload {
String test;
String other;
#Override
public String toString() {
return "Payload [test=" + test + ", other=" + other + "]";
}
}
If you assert this condition, then you can deserialize the json using a TypeToken> as token as danypata suggest... like:
public static void main(String args[]) throws InterruptedException {
String ff = "{\"1\":{" + "\"test\":\"1\"," + "\"other\":\"stuff\"" + "}," + "\"2\":{" + "\"test\":\"2\","
+ "\"other\":\"stuff2\"" + "}}";
Gson gson = new Gson();
Type mapType = new TypeToken<Map<String, Payload>>() {
}.getType();
Map<String, Payload> map = gson.fromJson(ff, mapType);
System.out.println(map);
for (Entry<String, Payload> entry : map.entrySet()) {
System.out.println(entry.getKey());
System.out.println(entry.getValue());
}
}
giving as result:
{1=Payload [test=1, other=stuff], 2=Payload [test=2, other=stuff2]}
1
Payload [test=1, other=stuff]
2
Payload [test=2, other=stuff2]
Why don't you use the Android JSONObject class? Then you can parse with it your entire JSON string and then you can obtain the values easily. For example this is to get the values of your "1" JSON object:
final JSONObject jsonObject = new JSONObject(jsonString);
final String test = jsonObject.getJSONObject("1").getString("test");
final String other = jsonObject.getJSONObject("1").getString("other");

Serialize Json into generic structure without schema using Java and Jackson

I have a need to serialize a JSON without being attached to particular schema for resulting object, e.g., to some generic set/map/hashmap.
As input, I have a string with a JSON. I do not know schema for that JSON.
As output, I want a Java Object such as Hashmap or similar that has key-value serialization of input.
Note that input JSON has both basic fields and Array/List inside it.
I have to use Java and Jackson (or some other library). How I possibly can do that?
Jackson data binding is able to read any json input into a Map with String key and Object value (that can be also a map or collection). You just tell the mapper that you would like to read the json into a map. You do that by giving the mapper the appropriate type reference:
import java.util.*;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Test
{
public static void main(String[] args)
{
try {
String json = "{ "
+ "\"string-property\": \"string-value\", "
+ "\"int-property\": 1, "
+ "\"bool-property\": true, "
+ "\"collection-property\": [\"a\", \"b\", \"c\"], "
+ "\"map-property\": {\"inner-property\": \"inner-value\"} "
+ "}";
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> map = new HashMap<>();
// convert JSON string to Map
map = mapper.readValue(json, new TypeReference<Map<String, Object>>(){});
System.out.println("input: " + json);
System.out.println("output:");
for (Map.Entry<String, Object> entry : map.entrySet()) {
System.out.println("key: " + entry.getKey());
System.out.println("value type: " + entry.getValue().getClass());
System.out.println("value: " + entry.getValue().toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
output:
input: { "string-property": "string-value", "int-property": 1, "bool-property": true, "collection-property": ["a", "b", "c"], "map-property": {"inner-property": "inner-value"} }
output:
key: string-property
value type: class java.lang.String
value: string-value
key: int-property
value type: class java.lang.Integer
value: 1
key: bool-property
value type: class java.lang.Boolean
value: true
key: collection-property
value type: class java.util.ArrayList
value: [a, b, c]
key: map-property
value type: class java.util.LinkedHashMap
value: {inner-property=inner-value}

Categories