[{"a":
{"b":"c",
"d":"e",
"f":"g"}
{"a2":
{"b":"c2",
"d":"e2",
"f":"g2"}]
This is the JSON data I 'have to' use. Is there any easy way for me to reach, let's say the b values without having to go through a and a2?
Use GSON
You should create a class for that:
public class MyObject {
private A a;
private A a2;
// a and a2 getters here to check if it parses successfully
}
Class A
public class A {
private String b;
private String d;
private String f;
// getters
}
And somewhere where you want to parse:
Type listType = new TypeToken<ArrayList<MyObject >>() {}.getType();
Gson gson = new Gson();
List<MyObject> list = gson.fromJson(yourJsonStringHere, listType);
Related
Edit
I managed to insert only the outer JSON object. Couldn't find information on how to insert nested objects. Each object has primitive fields in it. These objects can be seen in the JSON sample below: "languages" and "currencies". I also wonder how to deal with the "latlng" array within the outer JSON object, but it's probably better to handle these issues one at a time.
The code I got for inserting a primitive value in the outer JSON object:
Gson gson = new Gson();
Type listType = new TypeToken<List<Country>>(){}.getType();
List<Country> countriesList = gson.fromJson(jsonString, listType);
for(Country country : countriesList) {
ContentValues insertValues = new ContentValues();
insertValues.put(Country.NAME, country.getName());
//more insertions
}
The original question
I parsed a nested JSON with GSON (probably incorrectly since it's my first time). Now I'm trying to insert it to SQLite. Couldn't find what needs to be written next. When I write the for loop for the insertion I get the error cannot find symbol class Country. Couldn't find relevant guidance online so I hope anyone can help in how to move forward.
That's where the issue begins:
Country[] countriesArray = gson.fromJson(jsonString, Country[].class);
for (int i = 0; i < countriesArray.length(); i++) {
...
}
countriesArray.length() is marked as an error: cannot find symbol class Country.
One object from the JSON:
[
{
"name":"Afghanistan",
"topLevelDomain":[
".af"
],
"callingCodes":[
"93"
],
"capital":"Kabul",
"region":"Asia",
"subregion":"Southern Asia",
"population":27657145,
"latlng":[
33.0,
65.0
],
"demonym":"Afghan",
"area":652230.0,
"gini":27.8,
"timezones":[
"UTC+04:30"
],
"nativeName":"افغانستان",
"numericCode":"004",
"currencies":[
{
"name":"Afghan afghani",
"symbol":"؋"
}
],
"languages":[
{
"name":"Pashto",
"nativeName":"پښتو"
},
{
"name":"Uzbek",
"nativeName":"Oʻzbek"
},
{
"name":"Turkmen",
"nativeName":"Türkmen"
}
],
"translations":{
"de":"Afghanistan",
},
"flag":"https://restcountries.eu/data/afg.svg",
"cioc":"AFG"
},
The model classes I wrote are only for the variables objects and arrays I needed.
The model class Country.Java
public class Country implements Parcelable {
private String name;
private String capital;
private String region;
private String subregion;
private int population;
private List<Double> latlng = new ArrayList<Double>();
private double area;
private double gini;
private List<String> timezones = new ArrayList<String>();
private List<Currency> currencies = new ArrayList<Currency>();
private List<Language> languages = new ArrayList<Language>();
private String flag;
public Country() {}
//getters, setters, toString() and Parcelable methods
}
The model class Currency.Java
public class Currency implements Parcelable {
private String name;
private String symbol;
//getters, setters, toString() and Parcelable methods
}
The model class Language.Java
public class Language implements Parcelable {
private String name;
private String nativeName;
//getters, setters, toString() and Parcelable methods
}
Create a typeToken and pass it to fromJson method like following
Type listType = new TypeToken<List<Country>>(){}.getType();
List<Country> countryList = gson.fromJson(jsonArray.toString(), listType);
Use Gson TypeToken to set the return object type during JSON parse
List<Country> countries = gson.fromJson(jsonString, new TypeToken<List<Country>>(){}.getType());
for(Country country : countries) {
//Do your DB operation here
}
After parsing a nested JSON array with Gson, I now need to insert the result into SQLite. I tried inserting as done when not parsing with Gson, but that didn't work. I looked for ways to do that but couldn't find a solution.
The JSON parsing:
Gson gson = new Gson();
Type listType = new TypeToken<List<Country>>(){}.getType();
List<Country> countriesList = gson.fromJson(jsonString, listType);
for(Country country : countriesList) {
ContentValues insertValues;
}
If I wasn't using Gson, I would have written the line:
JSONObject countryObject = countriesList.getJSONObject(country);
EDIT
One of the objects from the JSON
[
{
"name":"Afghanistan",
"topLevelDomain":[
".af"
],
"callingCodes":[
"93"
],
"capital":"Kabul",
"region":"Asia",
"subregion":"Southern Asia",
"population":27657145,
"latlng":[
33.0,
65.0
],
"demonym":"Afghan",
"area":652230.0,
"gini":27.8,
"timezones":[
"UTC+04:30"
],
"nativeName":"افغانستان",
"numericCode":"004",
"currencies":[
{
"name":"Afghan afghani",
"symbol":"؋"
}
],
"languages":[
{
"name":"Pashto",
"nativeName":"پښتو"
},
{
"name":"Uzbek",
"nativeName":"Oʻzbek"
},
{
"name":"Turkmen",
"nativeName":"Türkmen"
}
],
"translations":{
"de":"Afghanistan",
},
"flag":"https://restcountries.eu/data/afg.svg",
"cioc":"AFG"
},
The model classes I wrote are only for the variables objects and arrays I needed.
The model class Country.Java
public class Country implements Parcelable {
private String name;
private String capital;
private String region;
private String subregion;
private int population;
private List<Double> latlng = new ArrayList<Double>();
private double area;
private double gini;
private List<String> timezones = new ArrayList<String>();
private List<Currency> currencies = new ArrayList<Currency>();
private List<Language> languages = new ArrayList<Language>();
private String flag;
public Country() {}
//getters, setters, toString() and Parcelable methods
}
The model class Currency.Java
public class Currency implements Parcelable {
private String name;
private String symbol;
//getters, setters, toString() and Parcelable methods
}
The model class Language.Java
public class Language implements Parcelable {
private String name;
private String nativeName;
//getters, setters, toString() and Parcelable methods
}
First get property of Country and then put it to content values and insert.
List<Country> countries = gson.fromJson(jsonString, new TypeToken<List<Country>>(){}.getType());
for(Country country : countries) {
String name = country.getName();
String capital = country.getCapital();
String region = country.getRegion();
String currencies = gson.toJson(country.getCurrencies());
...
ContentValues insertValues = new ContentValues();
insertValues.put("name", name)
insertValues.put("capital", capital);
insertValues.put("region", region);
insertValues.put("currencies", currencies);
...
long res = db.insert(TABLE_NAME, null, insertValues);
}
It's hard to know where your problem is without more code. You are doing two different operations:
unmarshalling a json
persisting data in SQLite.
Unmarshalling json with Gson
Inside the for-loop, can you log each country to see that you are getting a valid object with all the fields set? It is very much possible that you need to create a factory yourself. Does Country have subtype that you need to register through RuntimeTypeAdapterFactory? Maybe something like
final RuntimeTypeAdapterFactory<City> typeFactory = RuntimeTypeAdapterFactory
of(City.class, "MyCity")
.registerSubtype(S...,...)
Save in SQLite
Once you have valid data, then you must convert per field like so
public static ContentValues getContentValues(Country country) {
ContentValues values = new ContentValues();
values.put(COLUMN_ID, country.getId());
values.put(COLUMN_NAME, country.getName());
...
return values;
}
And then, if it still doesn't work, you will need to look at your SQLite schema.
I have three nested classes as the following:
public class A {
String s1;
String s2;
String s3;
String s4;
String s5;
String s6;
String s7;
String s8;
B b;
}
public class B{
private Map<String, C> ag = new HashMap<>();
}
public class C{
private Map<String, Boolean> ar = new HashMap<>();
}
Class A is sent as a GET method reply of type JSON to an Android app which consumes the service as shown bellow:
Gson gson = new GsonBuilder()
.enableComplexMapKeySerialization()
.setLenient()
.create();
Retrofit r = new Retrofit.Builder()
.baseUrl(url)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
...
However, when running the Android app, I get the following error:
E/Rest GET Error: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line ? column ? B.ag.
Having in mind that If I remove class b field from class A, everything goes smoothly. It seems that gson is unable to deserialize a composed JSON file. So what to do?
Note: The JSON file is retrieved successfully from a Restful webservice using MOXy not Jackson. So there is no option to use #JsonDeserialize.
Below is the returned JSON file:
{"b":{"ag":{"entry":[{"key":"AS2","value":{"ar":{"entry":[{"key":"s1","value":false},{"key":"s2","value":false},{"key":"s3","value":false},{"key":"s4","value":false},{"key":"s5","value":false},{"key":"s6","value":true},{"key":"s7","value":false},{"key":"s8","value":false}]}}},{"key":"AS3","value":{"ar":{"entry":[{"key":"s1","value":false},{"key":"s2","value":true},{"key":"s3","value":true},{"key":"s4","value":false},{"key":"s5","value":false},{"key":"s6","value":false},{"key":"s7","value":false},{"key":"s8","value":true}]}}},{"key":"AS1","value":{"ar":{"entry":[{"key":"s1","value":false},{"key":"s2","value":true},{"key":"s3","value":true},{"key":"s4","value":false},{"key":"s5","value":false},{"key":"s6","value":false},{"key":"s7","value":false},{"key":"s8","value":false}]}}}]}},"s1":"string1","s2":"string2","s3":"string3","s4":"string4","s5":"string5","s6":"string6","s7":"string7","s8":"string8"}
From your class and error provided it seem that you are getting Array at B b; in Class A. We can say exact problem if you share jsonResponse.
----Updated---
Change your Class B
Class B{
private Ag ag;
}
class Ag {
List<Entry> entry;
}
class Entry{
String key;
Value value;
}
class Value{
Ar ar;
}
class Ar{
List<ArEntry> entry;
}
class ArEntry{
String key;
Boolean value;
}
This is how your classes for the jsonResponse you provided.
You can convert any json response to pojo here or you can add Android Studio plugin here.
I list of objects of a custom class which i am trying to serialize using json but the values once serialized are 0 and not the actual values that are stored in the list.
MyCustom Class
public class CustomClass extends RealmObject {
#Expose()
#SerializedName("startID")
private int startMessageID;
#Expose()
#SerializedName("endID")
private int endMessageID;
#Expose(serialize = false)
private boolean syncing = false;
}
The following is what i use to serialize the list.
GsonBuilder builder = new GsonBuilder();
builder.excludeFieldsWithoutExposeAnnotation();
Gson gson = builder.create();
Type listType = new TypeToken<List<CustomClass >>() {
}.getType();
Log.i("Json", gson.toJson(syncModelList, listType));
The above code produces an output of
[{"endID":0,"startID":0},{"endID":0,"startID":0}]
The structure is correct but my values are lost,i have checked the values before serialization and they are correct and exist.
It's because Gson can't serialize a managed Realm object. You need to convert it to an unmanaged object first, like this:
new Gson().toJson(realm.copyFromRealm(managedModel));
Have a look at this answer for a full explanation Android: Realm + Retrofit 2 + Gson
I have a large group of json objects received from web server. I want to get all the data from all the json objects. For that How do I iterate through the json object so, that all the values can be stored on arraylist..
This is a sample model of my json object received from server. I need all the data (name and city) in two arraylists. For that how do I loop through the json objects. There is no way of getting the data as json array from the server. That's why I asked here. If it was json array, It would have been easier for me. So please help me..
[{"Name":"abin","City":"aa"},{"Name":"alex","City":"bb"},....... a large collection of json objects...]
You could use Gson and parse the string to a java object.
For example you have a class.
public class Location{
private String name;
private String city;
//getters and setters
}
and in your class you could just parse it to Location class
Gson gson=new Gson();
Location[] locations=gson.fromJson(jsonString,Location[].class);
after that you could loop through the locations
for(int i=0;i<locations.length;i++){
System.out.println(locations[i].getName());
}
if you need to separate the city from the name
ArrayList name=new ArrayList();
ArrayList city=new ArrayList();
for(int i=0;i<locations.length;i++){
name.add(locations[i].getName());
city.add(locations[i].getCity());
}
If you know the structure of your JSON String, then use google's Gson() (add the JAR to your project) to deserialize, in 3 easy steps:
Create the Entity class (whatever your object is, I'm giving "Person" as example).
public class Person {
#Expose //this is a Gson annotation, tells Gson to serialize/deserialize the element
#SerializedName("name") //this tells Gson the name of the element as it appears in the JSON string, so it can be properly mapped in Java class
private String name;
#Expose
#SerializedName("lastName")
private String lastName;
#Expose
#SerializedName("streetName")
private String streetName;
//getters and setters follow
}
Create the class into which you deserialize the JSON string. In my example, the JSON string is actually an array of Persons.
public class PersonsList extends ArrayList<Person> implements Serializable{
//nothing else here
}
If the JSON string has a named key, then you don't have to extend ArrayList:
public class PersonsList implements Serializable{
#Expose
#SerializedName("persons")
private ArrayList<Person> persons;
//getters / setters
}
Do the actual deserialization:
String json = "[{person1},{person2},{person3}]";//your json here
Gson gson = new Gson();
PersonsList personsList = gson.fromJson(json, PersonsList.class);
//then, depending on how you build PersonsList class, you iterate:
for(Person p : personsList)//if you extended ArrayList
//or
for(Person p : personsList.getPersons())//if it's the second option