nica
nica

Reputation: 51

Convert jsonobject to string by using gson issue

I'm try to convert JsonObject to String by using GSON library. But the result output will have one more layer of parent "map" wrap up the json. Please let me know any wrong i did why the layer of parent "Map" will appear?

i do even try to covert the bean by using new Gson().toJson(bean); but the output result also have one more layer of parent "map" wrap up the json.

Condition i need to fulfil by use

1) Mutable object
2) GSON
3) method might handle other object Type

Maven project using as bellow library:

<dependency>
  <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.5</version>
</dependency>

Java code bellow(example for understand only not the real code, will using in T):

List<JSONObject> records = new ArrayList <JSONObject> ();           
JSONObject bean = new JSONObject();

bean.put("A", "is A");    
bean.put("B", "is B lah");    
bean.put("C", "is C lah");    
bean.put("D", "is D");    

records.add(bean);

String JSONBody2 = new Gson().toJson(records);

I expect the output is [{"D":"is D","A":"is A","B":"is B lah","C":"is C lah"}]

but the actual output is [{"map":{"D":"is D","A":"is A","B":"is B lah","C":"is C lah"}}]

Actual code is as below

public String Json( String json, List<T>  list) {
   String JSONBody = new Gson().toJson(list);
}

I need to Serialization by using gson that's why i put the T. but i don't have idea why the "map" is appeared here. as previously it work without Parent "Map" wrap up. (same code and same library just new recreated project but having this issue)

Upvotes: 5

Views: 24516

Answers (4)

Manish Bansal
Manish Bansal

Reputation: 2681

Although others have already answered it, i would like to highlight one important learning. There are 2 ways to convert JsonObject to string. One is new Gson().toJson(..) and other is JsonObject.toString().

Although both will produce same results in many cases but if the JsonObject has some string fields containing valid urls or base64 ecoded values, the first method will convert & and = into corresponding utf-8 representative characters which will make your urls and base64 ecoded values corrupted. However, second method keep those values intact.

Upvotes: 3

Aditya
Aditya

Reputation: 2246

Don't use a JSON Object. Just use Object

    List<Object> records = new ArrayList<>();
    Map<String, String> bean = new HashMap<>();

    bean.put("A", "is A");
    bean.put("B", "is B lah");
    bean.put("C", "is C lah");
    bean.put("D", "is D");
    records.add(bean);


    String JSONBody2 = new Gson().toJson(records);
    System.out.println(JSONBody2);

Output is

[{"A":"is A","B":"is B lah","C":"is C lah","D":"is D"}]

If you look at the implementation of org.json.JSONObject, it internally contains a variable called map where it stores all the data. This is the constructor

public JSONObject() {
    this.map = new HashMap<String, Object>();
}

When GSON tries to convert to JSON, it just recursively looks into the object and converts all variables to JSON. So in this case, it converts the internal map, which shows up in the Json output.

Upvotes: 0

Veena
Veena

Reputation: 869

try

  String JSONBody2 = record.toString());

will give you [{"A":"is A","B":"is B lah","C":"is C lah","D":"is D"}]

You can get more better understanding of type conversion from this link https://stackoverflow.com/a/27893392/4500099

Upvotes: 4

Amit Rai
Amit Rai

Reputation: 407

Use JSONArray instead of List , it will give you the desired output:

JSONArray  records = new JSONArray();           
JSONObject bean = new JSONObject();

bean.put("A", "is A");
bean.put("B", "is B lah");    
bean.put("C", "is C lah");    
bean.put("D", "is D");    

records.put(bean);

String JSONBody2 = new Gson().toJson(records);

Upvotes: 1

Related Questions