Can't get correct json from hashmap and gson - java

I am fetching some json from a pojo class using GSON and hashmap. This is Pojo Class
public class NetworkConfiguration {
#SerializedName("GUID")
#Expose
private String gUID;
#SerializedName("Name")
#Expose
private String name;
#SerializedName("Type")
#Expose
private String type;
#SerializedName("WiFi")
#Expose
private WiFi wiFi;
public NetworkConfiguration() {
}
public NetworkConfiguration(String gUID, String name, String type, WiFi wiFi) {
super();
this.gUID = gUID;
this.name = name;
this.type = type;
this.wiFi = wiFi;
}
public String getGUID() {
return gUID;
}
public void setGUID(String gUID) {
this.gUID = gUID;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public WiFi getWiFi() {
return wiFi;
}
public void setWiFi(WiFi wiFi) {
this.wiFi = wiFi;
}
}
This is how I am trying to get JSON
WiFi wiFi = new WiFi();
wiFi.setPassphrase(open_network_configuration_wifi_password_edit.getText().toString());
wiFi.setSecurity(spinner_open_network_configuration_wifi_security.getSelectedItem().toString());
wiFi.setSSID(open_network_configuration_wifi_ssid_edit.getText().toString());
if (open_network_configuration_wifi_autoconnect_option.isChecked()) {
wiFi.setAutoConnect(true);
} else {
wiFi.setAutoConnect(false);
}
Map<String, Object> openNetworkConfigurationMap = new HashMap<>();
networkConfigurations.add(new NetworkConfiguration(open_network_configuration_guid_edit.getText().toString(),
open_network_configuration_name_edit.getText().toString(), "WiFi", wiFi));
Gson gson = new Gson();
String json = gson.toJson(networkConfigurations);
Map<String,Object> result = new Gson().fromJson(json, Map.class);
openNetworkConfigurationMap.put("NetworkConfigurations", json);
And this is what I am getting
NetworkConfigurations= [{
"GUID": "a",
"Name": "Example A",
"Type": "WiFi",
"WiFi": {
"SSID": "Example A",
"Security": "None",
"AutoConnect": true
}
}]
And this is wrong. This is what I am expecting
"NetworkConfigurations": [{
"GUID": "a",
"Name": "Example A",
"Type": "WiFi",
"WiFi": {
"SSID": "Example A",
"Security": "None",
"AutoConnect": true
}
}]
The main thing which is kinda worry that I am getting an equal sign = but it should be : after the NetworkConfigurations
Any kind of help would be appreciated. Thanks in advance

Just add this in the end:
String json = gson.toJson(openNetworkConfigurationMap);
and print the output json

Related

De-serializing a JSON GET Request Response in Java

De-Serializing Objects within a List
I currently have an 'Admin' Entity in which upon a GET Request of AllAdmin() will return me the following response. This was used in Postman.
GET Response for AllAdmin() [POSTMAN]
[
{
"adminId": 1,
"fullName": "Patrick ",
"email": "patrick#gmail.com",
"dob": "1669-12-12",
"mobileNumber": "96369636",
"password": "password123!",
"usages": [
{
"id": 3,
"datetimeUnlocked": "2021-06-07 10:12:23"
},
{
"id": 4,
"datetimeUnlocked": "2021-06-07 10:12:27"
}
],
"authorization": [
{
"id": 2,
"datetimeAccepted": "2021-06-07 10:12:14"
}
],
"adminAllow": []
},
{
"adminId": 2,
"fullName": "Worker ",
"email": "worker#gmail.com",
"dob": "2000-12-12",
"mobileNumber": "96399639",
"password": "password123!",
"usages": [],
"authorization": [],
"adminAllow": []
} ]
The current code is my Admin Model in my Android Application.
Admin.java Model Class
public class Admin {
#SerializedName("adminId")
private long adminID;
#SerializedName("fullName")
private String adminFullName;
#SerializedName("email")
private String adminEmail;
#SerializedName("dob")
private String adminDOB;
#SerializedName("mobileNumber")
private String adminMobileNumber;
// Constructor
public Admin(long adminID, String adminFullName, String adminEmail, String adminDOB, String adminMobileNumber) {
this.adminID = adminID;
this.adminFullName = adminFullName;
this.adminEmail = adminEmail;
this.adminDOB = adminDOB;
this.adminMobileNumber = adminMobileNumber;
}
// Getter
public long getAdminID() {
return adminID;
}
public String getAdminFullName() {
return adminFullName;
}
public String getAdminEmail() {
return adminEmail;
}
public String getAdminDOB() {
return adminDOB;
}
public String getAdminMobileNumber() {
return adminMobileNumber;
}
}
I would like to clarify on how I would de-serialize the usages and authorization properties so that I am able to access and manipulate the data entries for these?
I thank you in advance for your clarifications!
For usages, you should use a List<Usage> as the serializable. My assumption is you are using Retrofit. So the Retrofit Gson converter will take care of parsing the array of Usage. The same logic would apply for authorization. You can use the different key there for the datetimeAccepted case.
Check this sample for Usage
public class Usage {
#SerializedName("id")
#Expose
private Integer id;
#SerializedName("datetimeUnlocked")
#Expose
private String datetimeUnlocked;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDatetimeUnlocked() {
return datetimeUnlocked;
}
public void setDatetimeUnlocked(String datetimeUnlocked) {
this.datetimeUnlocked = datetimeUnlocked;
}
}
Your updated Admin class must look like this
public class Admin {
#SerializedName("adminId")
private long adminID;
#SerializedName("fullName")
private String adminFullName;
#SerializedName("email")
private String adminEmail;
#SerializedName("dob")
private String adminDOB;
#SerializedName("mobileNumber")
private String adminMobileNumber;
#SerializedName("usages")
private List<Usage> usages;
// Constructor
public Admin(long adminID, String adminFullName, String adminEmail, String adminDOB, String adminMobileNumber, List<Usage> usages) {
this.adminID = adminID;
this.adminFullName = adminFullName;
this.adminEmail = adminEmail;
this.adminDOB = adminDOB;
this.adminMobileNumber = adminMobileNumber;
this.usages = usages;
}
// Getter
public long getAdminID() {
return adminID;
}
public String getAdminFullName() {
return adminFullName;
}
public String getAdminEmail() {
return adminEmail;
}
public String getAdminDOB() {
return adminDOB;
}
public String getAdminMobileNumber() {
return adminMobileNumber;
}
public List<Usage> getUsages() {
return usages;
}
}

Gson: Expected a string but was BEGIN_OBJECT at line 1 column 3 path $[0]

I want to get an ArrayList out of a String containing:`
[
{
"name": "*",
"package": "permission",
"id": "permission.*"
},
{
"name": "*",
"package": "info",
"id": "info.*"
},
{
"name": "help",
"package": "info",
"id": "info.help"
},
{
"name": "ping",
"package": "info",
"id": "info.ping"
},
{
"name": "premium",
"package": "info",
"id": "info.premium"
}
]
However, I get a JsonSyntaxException every time. My Permission.class looks like this:
public enum Permission {
ALL("permission", "*"),
INFO("info", "*"),
INFO_HELP("info", "help"),
INFO_PING("info", "ping"),
INFO_PREMIUM("info", "premium"),
;
#SerializedName("permPack")
public final String permPack;
#SerializedName("permName")
public final String permName;
#SerializedName("permId")
public final String id;
Permission(String permPack, String name) {
this.permPack = permPack;
this.permName = name;
this.id = permPack + "." + name;
}
public String getId() {
return id;
}
public String getName() {
return permName;
}
public String getPermPack() {
return permPack;
}
public static Permission getById(String Id) {
List<Permission> perms = Arrays.asList(Permission.values());
for (int i = 0; i < perms.size(); i++) {
if (perms.get(i).getId().equals(Id)){
return perms.get(i);
}
}
return null;
}
}
I tried to deserialize the String using:
Type listType = new TypeToken<ArrayList<Permission>>(){}.getType();
ArrayList<Permission> availablePermsList = gson.fromJson(permissionString, listType);
I also tried to add a name to this String or writing an own deserializer using Permission.getById, nothing works. I am out of ideas.
This is how your POJO Class should look like instead of enum it should be plain java class
class Permission {
private String id;
private String name;
#SerializedName("package")
private String packageName;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
#Override
public String toString() {
return "Permission{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", packageName='" + packageName + '\'' +
'}';
}
}
For answering i have used JsonReader from a file, instead you can directly use
string in gson.fromJson(...) method
This is how i am calling to convert Array of string to Permission List
Type permissionsListType = new TypeToken<ArrayList<Permission>>(){}.getType();
JsonReader jsonReader = new JsonReader(new FileReader(new File("permissions.json")));
Gson gson = new Gson();
ArrayList<Permission> availablePermsList = gson.fromJson(jsonReader, permissionsListType);
System.out.println(availablePermsList);

Get nested JSON object with GSON : Deserialization

I have gone through the threads from SOF which talks about getting nested JSON using GSON. Link 1 Link 2. My JSON file is as shown below
{
"Employee_1": {
"ZipCode": 560072,
"Age": 50,
"Place": "Hawaii",
"isDeveloper": true,
"Name": "Mary"
},
"Employee_2": {
"ZipCode": 560072,
"Age": 80,
"Place": "Texas",
"isDeveloper": true,
"Name": "Jon"
}
}
my classes are as shown below
public class Staff {
String Employee_1 ;
}
class addnlInfo{
String Name;
String Place;
int Age;
int Zipcode;
boolean isDeveloper;
}
The deserializer class which I built is as shown below
class MyDeserializer implements JsonDeserializer<addnlInfo>{
public addnlInfo deserialize1(JsonElement je, Type type, JsonDeserializationContext jdc)
throws JsonParseException
{
// Get the "content" element from the parsed JSON
JsonElement content = je.getAsJsonObject().get("Employee_1");
// Deserialize it. You use a new instance of Gson to avoid infinite recursion
// to this deserializer
return new Gson().fromJson(content, addnlInfo.class);
}
#Override
public TokenMetaInfo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
throws JsonParseException {
// TODO Auto-generated method stub
return null;
}
The main file
Gson gson = new GsonBuilder()
.registerTypeAdapter(addnlInfo.class, new MyDeserializer())
.create();
String jsonObject= gson.toJson(parserJSON);
addnlInfo info= gson.fromJson(jsonObject, addnlInfo .class);
System.out.println(info.Age + "\n" + info.isDeveloper + "\n" + info.Name + "\n" + info.Place);
Staff parentNode = gson.fromJson(jsonObject, Staff.class);
System.out.println(parentNode.Employee_1);
The problem:
My Subparent element (e.g. 'Employee_1') keeps changing. Do I have to construct multiple deserializers?
Also, I get "Expected a string but was BEGIN_OBJECT" which I understand as we use nestedJSON.
I am not sure how your classes translate to your JSON, but you are making this too complex.
I renamed fields and class names to adhere to Java standards.
Main.java
import java.lang.reflect.Type;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
public class Main {
public static void main(String[] args) {
Map<String, Staff> employees = new LinkedHashMap<String, Staff>();
employees.put("Employee_1", new Staff(new Info("Mary", "Hawaii", 50, 56072, true)));
employees.put("Employee_2", new Staff(new Info("Jon", "Texas", 80, 56072, true)));
String jsonString = new GsonBuilder().setPrettyPrinting().create().toJson(employees);
System.out.println("# SERIALIZED DATA:");
System.out.println(jsonString);
Type mapOfStaff = new TypeToken<Map<String, Staff>>() {}.getType();
Map<String, Staff> jsonObject = new Gson().fromJson(jsonString, mapOfStaff);
System.out.println("\n# DESERIALIZED DATA:");
for (Entry<String, Staff> entry : jsonObject.entrySet()) {
System.out.printf("%s => %s%n", entry.getKey(), entry.getValue());
}
}
}
Staff.java
public class Staff {
private Info info;
public Staff(Info info) {
this.info = info;
}
public Info getInfo() {
return info;
}
public void setInfo(Info info) {
this.info = info;
}
#Override
public String toString() {
return String.format("Staff [info=%s]", info);
}
}
Info.java
public class Info {
private String name;
private String place;
private int age;
private int zipcode;
private boolean developer;
public Info(String name, String place, int age, int zipcode, boolean developer) {
this.name = name;
this.place = place;
this.age = age;
this.zipcode = zipcode;
this.developer = developer;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPlace() {
return place;
}
public void setPlace(String place) {
this.place = place;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getZipcode() {
return zipcode;
}
public void setZipcode(int zipcode) {
this.zipcode = zipcode;
}
public boolean isDeveloper() {
return developer;
}
public void setDeveloper(boolean developer) {
this.developer = developer;
}
#Override
public String toString() {
return String.format(
"Info [name=%s, place=%s, age=%d, zipcode=%d, developer=%b]",
name, place, age, zipcode, developer
);
}
}
Output
# SERIALIZED DATA:
{
"Employee_1": {
"info": {
"name": "Mary",
"place": "Hawaii",
"age": 50,
"zipcode": 56072,
"developer": true
}
},
"Employee_2": {
"info": {
"name": "Jon",
"place": "Texas",
"age": 80,
"zipcode": 56072,
"developer": true
}
}
}
# DESERIALIZED DATA:
Employee_1 => Staff [info=Info [name=Mary, place=Hawaii, age=50, zipcode=56072, developer=true]]
Employee_2 => Staff [info=Info [name=Jon, place=Texas, age=80, zipcode=56072, developer=true]]

GSON to parse a JSON array

I have a JSON file like this:
[
{
"id": "1",
"name": "test1",
"childrens": [
{
"id": "14",
"name": "test2",
"childrens": [
]
}
]
}
]
The model class:
public class Model {
private int id;
private String name;
}
And my parse method:
public List< Model > parseJSONService(JSONArray jsonArray) {
Gson gson = new Gson();
Model[] model = gson.fromJson(jsonArray.toString(),
Model[].class);
return Arrays.asList(model);
}
why not parse the json string directly? this is what i did, achieved the same thing you looking for i guess:
public class GsonPlay {
public static void main(String args[]) {
String testString = "[{\"id\": \"1\",\"name\": \"test1\",\"childrens\": [{\"id\": \"14\",\"name\": \"test2\",\"childrens\": []}]}]";
List<Model> modelList = parseJsonService(testString);
System.out.println(modelList);
}
private static List<Model> parseJsonService(String testString) {
Gson gson = new Gson();
Model[] models = gson.fromJson(testString, Model[].class);
return Arrays.asList(models);
}
}
class Model {
private int id;
private String name;
private List<Model> childrens;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Model> getChildrens() {
return childrens;
}
public void setChildrens(List<Model> childrens) {
this.childrens = childrens;
}
}
You can also have a look at this for further ideas:

exception while convert string to jsonobject

I have a String in my servlet which is of the following format.
{
"name": "Jam",
"noOfBooksRequired": "2",
"type": "Type 1",
"bookName": [
"The Magic",
"The Power"
]
}
where the bookName is an array. I want to access the values in the array and populate in the bean. But, when I try to convert the string to jsonobject, I am getting the following exception because bookName is an array com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_ARRAY This is how I am trying to do it
JSONObject js= new JSONObject();
String inputData= request.getParameter("inputData");
HashMap<String, String> hmap= new HashMap<String, String>();
Type type = new TypeToken<HashMap<String, String>>(){}.getType();
hmap = gson.fromJson(inputData, type);
js.putAll(hmap);
What I am doing is, I convert the string to a map and then add it to the JSONObject.
Since there are many json serializers and not sure which is the best. Right now, I have net.sf.json.JSONObject and com.google.gson.JsonObject
Can someone help me to get this solved.
Thanks in advance
You can map your JSON to a POJO.
If the book will have more attributes besides the name, you'll need two POJOs, as you can see below.
A POJO for the book:
class Book {
private String name;
private String author;
public Book() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
And a POJO for the shelf, which have a list of books:
class Shelf {
private String name;
private Integer noOfBooksRequired;
private String type;
private List<Book> books;
public Shelf() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getNoOfBooksRequired() {
return noOfBooksRequired;
}
public void setNoOfBooksRequired(Integer noOfBooksRequired) {
this.noOfBooksRequired = noOfBooksRequired;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public List<Book> getBooks() {
return books;
}
public void setBooks(List<Book> books) {
this.books = books;
}
}
Your JSON will look like this:
{
"name": "Jam",
"noOfBooksRequired": "2",
"type": "Type 1",
"books": [
{"name": "The Magic", "author": "John Doe"},
{"name": "The Power", "author": "Jane Doe"}
]
}
And then you can use Gson to parse your JSON:
Gson gson = new Gson();
Shelf shelf = gson.fromJson(inputData, Shelf.class);
Update
Considering your JSON looks like this (the book can be represented as a String):
{
"name": "Jam",
"noOfBooksRequired": "2",
"type": "Type 1",
"books": [
"The Magic",
"The Power"
]
}
Only one POJO with a list of String is enough:
class Shelf {
private String name;
private Integer noOfBooksRequired;
private String type;
private List<String> books;
public Shelf() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getNoOfBooksRequired() {
return noOfBooksRequired;
}
public void setNoOfBooksRequired(Integer noOfBooksRequired) {
this.noOfBooksRequired = noOfBooksRequired;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public List<String> getBooks() {
return books;
}
public void setBooks(List<String> books) {
this.books = books;
}
}

Categories

Resources