Gson: Directly Convert String to JSONobject (No Pojo)

Gson: Directly convert String to JsonObject (no POJO)

use JsonParser; for example:

JsonObject o = JsonParser.parseString("{\"a\": \"A\"}").getAsJsonObject();

How to convert a String to JsonObject using gson library

You can convert it to a JavaBean if you want using:

 Gson gson = new GsonBuilder().setPrettyPrinting().create();
gson.fromJson(jsonString, JavaBean.class)

To use JsonObject, which is more flexible, use the following:

String json = "{\"Success\":true,\"Message\":\"Invalid access token.\"}";
JsonParser jsonParser = new JsonParser();
JsonObject jo = (JsonObject)jsonParser.parse(json);
Assert.assertNotNull(jo);
Assert.assertTrue(jo.get("Success").getAsString());

Which is equivalent to the following:

JsonElement jelem = gson.fromJson(json, JsonElement.class);
JsonObject jobj = jelem.getAsJsonObject();

How to convert jsonString to JSONObject in Java

Using org.json library:

try {
JSONObject jsonObject = new JSONObject("{\"phonetype\":\"N95\",\"cat\":\"WP\"}");
}catch (JSONException err){
Log.d("Error", err.toString());
}

Parsing String to JsonObject using GSON gives IllegalStateException: This is not a JSON Object

It's because due to the JSON structure..
I have to put it into a JSONObject first, like so

JsonObject jsonObj = new JsonObject();
jsonObj.addProperty(ServerConstants.JSONoutput, gson.toJson(roomList));

Then I would deserialize like

 List<RoomData> roomList = gson.fromJson(jsonObj.get(CardGameConstants.JSONoutput).toString(), listType);
for (RoomData roomData : roomList) {
System.out.println(roomData.getRoomID());
}

Unable to convert Gson JsonObject to POJO class

Your json elements are different from the POJO members as a result proper mapping is not happening.
Please annotate formattedId as

@SerializedName("FormattedID")
private String formattedId;

to make formattedId work.

How to use Google/GSON to convert a JSON string into Java POJO?

Generate your POJO by using http://www.jsonschema2pojo.org/ or by adding plugin in studio (https://github.com/Hexara/Json2Pojo)

now add dependencies compile 'com.google.code.gson:gson:2.6.2':

convert your json or string to POJO

Gson gson = new Gson();
POJOClass pojo = gson.fromJson(jsonObject.toString(), new TypeToken<POJOClass>() {}.getType());

How to convert POJO object to json string using gson for a specified field?

Remove this code from constructor :

public User() {
this.email="";
this.password="";
this.password_confirmation="";
this.token="";
}

If you want to exclude empty values from output json, you should make it as null.

Good luck!

Convert JSON to POJO class having multi words as key with Gson

As commented you need to use @SerializedName not omly because "many word keys" but for example as ffcd is in different case in your JSON. Annotating names exactly like in your JSON Gson is able to parse it:

@Getter @Setter
class SponsorMandate {
@SerializedName("Cell No")
private String cellNo;
@SerializedName("Landline No")
private String landlineNo;
@SerializedName("FFCD")
private String ffcd;
@SerializedName("Utility Code")
private String utilityCode;
@SerializedName("Email ID")
private String emailId;
@SerializedName("Change Reason")
private String changeReason;
@SerializedName("Category Code")
private String categoryCode;
}


Related Topics



Leave a reply



Submit