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
}
Related
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.
Giving
JSON
// imagine this is JSON of a city
{
"title" : "Troy"
"people" : [
{
{
"title" : "Hector",
"status" : "Dead"
},
{
"title" : "Paris",
"status" : "Run Away"
}
},
...
],
"location" : "Mediteranian",
"era" : "Ancient",
...
}
City
public class City {
#SerializeName("title")
String title;
#SerializeName("people")
List<Person> people;
#SerializeName("location")
String location;
#SerializeName("era")
String era;
...
}
Person
public class Person {
#SerializeName("title")
private String title;
#SerializeName("status")
private String status;
}
If having string of JSON above, it is possible to create list of person
A. without having to deserialize City first like following
City city = new Gson().fromJson(json, City.class)
ArrayList<Person> people = city.people;
And
B. without having to convert string to JSONObject, get JSONArray and then convert back to string like following
String peopleJsonString = json.optJSONArray("people").toString
ArrayList<Person> people = new Gson().fromJSON(peopleJsonString, Person.class);
You can use a custom JsonDeserializer, which is part of Gson (com.google.gson.JsonDeserializer).
Simple example:
public class WhateverDeserializer implements JsonDeserializer<Whatever> {
#Override
public Whatever deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context) throws JsonParseException {
Whatever whatever = new Whatever();
// Fetch the needed object here
// whatever.setNeededObject(neededObject);
return whatever;
}
}
You can then apply this deserializer like this:
Gson gson = new GsonBuilder()
.registerTypeAdapter(Whatever.class, new WhateverDeserializer())
.create();
There is a full example of how to use a custom deserializer, including a super detailed explanation, on this page: http://www.javacreed.com/gson-deserialiser-example/
I don't think you can get the list directly without parsing the json array. You need to parse the array. And it would be faster via Gson;
If you strictly need (only array) and you won't be using any other json object . Simply delete them, so that gson won't parse them.
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
[{"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);
How can I parse this JSON using Gson?
I have an array with multiple object types and I don't know what kind of object I need to create to save this structure. I cannot change the json message (I don't control the server).
The only class that function (sort of) was this
public class Response {
private List<Object> tr;
private int results;
(...)
}
JSON Message (Note the array with multiple object types.)
{
"tr":
[
{
"a":
{
"userId": "112"
}
},
{
"b":
{
"userId": "123",
"address":"street dummy"
}
},
{
"a":
{
"userId": "154"
}
}
],
"results":3
}
The Gson User's Guide explicitly covers this:
https://sites.google.com/site/gson/gson-user-guide#TOC-Serializing-and-Deserializing-Collection-with-Objects-of-Arbitrary-Types
You have an object with a field tr that is an array containing arbitrary types.
The users guide explains that you can't directly deserialize such a structure, and recomends:
Use Gson's parser API (low-level streaming parser or the DOM parser
JsonParser) to parse the array elements and then use Gson.fromJson()
on each of the array elements. This is the preferred approach.
In your case ... it would really depend on what objects were possible in that array. If they are all going to have that same inner object you'd want to do something like...
List<MyUserPojo> list = new ArrayList<MyUserPojo>();
JsonArray array = parser.parse(json).getAsJsonObject().getAsJsonArray("tr");
for (JsonElement je : array)
{
Set<Map.Entry<String,JsonElement>> set = je.getAsObject().entrySet();
JsonElement je2 = set.iterator().next().getValue();
MyUserPojo mup = new Gson().fromJson(je2, MyUserPojo.class);
list.add(mup);
}
And of course, this would need to be inside a custom deserializer for your actual object that would have the tr and results fields.
class MyPojo
{
List<MyUserPojo> userList;
int results;
}
class MyUserPojo
{
String userId;
String address;
}
class MyDeserializer implements JsonDeserializer<MyPojo>
{
#Override
public MyPojo deserialize(JsonElement je, Type type, JsonDeserializationContext jdc)
throws JsonParseException
{
List<MyUserPojo> list = new ArrayList<MyUserPojo>();
JsonArray array = je.getAsJsonObject().getAsJsonArray("tr");
for (JsonElement je2 : array)
{
Set<Map.Entry<String,JsonElement>> set = je2.getAsObject().entrySet();
JsonElement je3 = set.iterator().next().getValue();
MyUserPojo mup = new Gson().fromJson(je3, MyUserPojo.class);
list.add(mup);
}
MyPojo mp = new MyPojo();
mp.tr = list;
mp.results = je.getAsObject().getAsJsonPrimitive("results").getAsInt();
return mp;
}
}
Now you're all set - you can use that deserializer and create your object:
Gson gson = new GsonBuilder()
.registerTypeAdapter(MyPojo.class, new MyDeserializer())
.build();
MyPojo mp = gson.fromJson(json, MyPojo.class);
If the a, b etc are important ... well, you'll have to figure that out. But the above should get you well on your way to understanding what's going to be needed to deal with your JSON structure.
For completeness sake, the only "hacky" way around this is if there is a fairly limited number of those types and the inner object also is fairly limited in terms of its fields. You could create a POJO that encompasses all the possibilities:
class MyPojo
{
MySecondPojo a;
MySecondPojo b;
...
MySecondPojo f;
}
class MySecondPojo
{
String userId;
String address;
...
String someOtherField;
}
When Gson deserializes JSON it will set any missing fields in your POJO(s) to null. You could now have tr be a List or array of these in your POJO. Again and to emphasize, this is really quite hacky and the wrong way to do it, but I thought I'd explain what would be required to directly parse that array.
I pick something from each answer and did it this way:
Response Object
public class Response {
private List<Users> tr;
private int results;
(...)
}
Generic User
public class User {
public static final int TYPE_USER_A =0;
public static final int TYPE_USER_B =1;
private String userId;
private int type;
(...)
}
A
public class a extends User {
private String location;
(...)
}
B
public class b extends User {
private String adress;
(...)
}
Parsing Method
private Response buildResponseObject(String response) {
Response tls = new Response();
List<Users> users = new ArrayList<users>();
User u;
try {
JSONObject object = new JSONObject(response);
tls.setResults(object.getInt("results"));
JSONArray array = object.getJSONArray("tr");
for (int i = 0; i < array.length(); i++) {
JSONObject trs = array.getJSONObject(i);
if (trs.has("a")) {
String json = trns.getString("a");
A a = new Gson().fromJson(json,A.class);
a.setType(User.TYPE_USER_A);
users.add(a);
} else if (trs.has("b")) {
String json = trs.getString("b");
B b= new Gson().fromJson(json,B.class);
B.setType(User.TYPE_USER_B);
users.add(b);
}
}
tls.setUsers(users);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return tls;
}
This is not as elegant as I wanted and mix native JsonObjects with Gson methods but works for me.
Try this code here:
public class Address {
public String userId;
public String address;
// ...
}
public class Response {
private HashMap<String, Address> tr;
private int results;
// ...
}
Usage:
String json = "{\n \"tr\":\n {\n \"a\": {\n \"userId\": \"112\"\n },\n \"b\": {\n \"userId\": \"123\",\n \"address\":\"street dummy\"\n },\n \"c\": {\n \"userId\": \"154\"\n }\n },\n \"results\":3\n}";
Response users = new Gson().fromJson(json, Response.class);
As you may see I needed to modify the structure:
{
"tr":
{
"a": {
"userId": "112"
},
"b": {
"userId": "123",
"address":"street dummy"
},
"c": {
"userId": "154"
}
},
"results":3
}
But unfortunately I don't get it managed to allow multiple keys. Right now I have no idea how to fix this.
I think this link might help you:
https://sites.google.com/site/gson/gson-user-guide#TOC-Collections-Examples
Basically, create a class for your "object" (kind of user I guess), and then use the deserialization code of Gson, like this:
Type collectionType = new TypeToken<Collection<User>>(){}.getType();
Collection<User> users= gson.fromJson(json, collectionType);
You can create corresponding java classes for the json objects. The integer, string values can be mapped as is. Json can be parsed like this-
Gson gson = new GsonBuilder().create();
Response r = gson.fromJson(jsonString, Response.class);
Here is an example- http://rowsandcolumns.blogspot.com/2013/02/url-encode-http-get-solr-request-and.html