I am using Spring Social FqlQuery to get data's from facebook. Here is the JSON response I am getting from facebook. My controller where i am getting Json output is here,
fql = "SELECT work FROM user WHERE uid = me()";
facebook.fqlOperations().query(fql, new FqlResultMapper<Object>() {
public Object mapObject(FqlResult result) {
List list = (List) result.getObject("work");
for (Object object : list) {
JsonHelper jsonHelper = new JsonHelper();
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String jsonOutput = gson.toJson(object);
System.out.println(jsonOutput);
gson.fromJson(jsonOutput, JsonHelper.class);
}
System.out.println inside for loop Outputs multiple json as below.:
{
"employer": {
"id": 129843057436,
"name": "www.metroplots.com"
},
"location": {
"id": 102186159822587,
"name": "Chennai, Tamil Nadu"
},
"position": {
"id": 108480125843293,
"name": "Web Developer"
},
"start_date": "2012-10-01",
"end_date": "2013-05-31"
}
{
"employer": {
"id": 520808381292985,
"name": "Federation of Indian Blood Donor Organizations"
},
"start_date": "0000-00",
"end_date": "0000-00"
}
Here is my Helper Class:
import java.util.List;
public class JsonHelper {
class Employer{
private int id;
private String name;
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;
}
}
class Location{
private int id;
private String name;
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;
}
}
class Position{
private int id;
private String name;
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;
}
}
//Edited After here
private String start_Date;
private String end_Date;
private Employer employer;
private Location location;
private Position position;
public String getStart_Date() {
return start_Date;
}
public void setStart_Date(String start_Date) {
this.start_Date = start_Date;
}
public String getEnd_Date() {
return end_Date;
}
public void setEnd_Date(String end_Date) {
this.end_Date = end_Date;
}
public Employer getEmployer() {
return employer;
}
public void setEmployer(Employer employer) {
this.employer = employer;
}
public Location getLocation() {
return location;
}
public void setLocation(Location location) {
this.location = location;
}
public Position getPosition() {
return position;
}
public void setPosition(Position position) {
this.position = position;
}
}
When I try to convert the json objects to java object as done above I am getting this exception.
HTTP Status 500 - Request processing failed; nested exception is com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 6 column 16
Can any one help me where I am wrong. Please help me converting json to java objects. Hope my question is clear. Thanks in advance.
EDIT MADE TO CONTROLLER:
facebook.fqlOperations().query(fql, new FqlResultMapper<Object>() {
public Object mapObject(FqlResult result) {
List<JsonHelper> json = new ArrayList<JsonHelper>();
List list = (List) result.getObject("work");
for (Object object : list) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
String jsonOutput = gson.toJson(object);
System.out.println(jsonOutput);
JsonHelper jsonHelper = gson.fromJson(jsonOutput, JsonHelper.class);
json.add(jsonHelper);
System.out.println(jsonHelper.getStart_Date());
}
for (JsonHelper jsonHelper : json) {
System.out.println(jsonHelper.getStart_Date());
}
return list;
}
});
Since i am not having the actual api access, so i am trying it with static value in the example. Firstly in your JsonHelper class, replace all int by long , as the values mentioned in the json are of type long and String. Then try it like mentioned below:
String str = "{\n"
+ " \"employer\": {\n"
+ " \"id\": 129843057436,\n"
+ " \"name\": \"www.metroplots.com\"\n"
+ " },\n"
+ " \"location\": {\n"
+ " \"id\": 102186159822587,\n"
+ " \"name\": \"Chennai, Tamil Nadu\"\n"
+ " },\n"
+ " \"position\": {\n"
+ " \"id\": 108480125843293,\n"
+ " \"name\": \"Web Developer\"\n"
+ " },\n"
+ " \"start_date\": \"2012-10-01\",\n"
+ " \"end_date\": \"2013-05-31\"\n"
+ "}";
List<JsonHelper> json = new ArrayList<JsonHelper>();
Gson gson = new Gson();
JsonHelper users = gson.fromJson(str, JsonHelper.class);
json.add(users);
for (JsonHelper js_obj : json) {
System.out.println(js_obj.getEmployer().getId());
System.out.println(js_obj.getEmployer().getName());
}
Related
I'm making a Shopping app which gets product attributes from the server. The Json Array I get from the server contains nested Json objects and Json arrays which look likes this:
[
"id": 1860,
"name": "T-Shirt",
"attributes": [
{
"id": 1,
"name": "color",
"position": 0,
"visible": true,
"variation": false,
"options": [
"blue",
"green",
"red"
]
},
{
"id": 2,
"name": "size",
"position": 3,
"visible": true,
"variation": false,
"options": [
"L",
"M",
"XL",
"XXL"
]
}
],
I created a class for managing product variables which contains of strings and ints and setters and getters for simple variable types like name,price etc.
public class Product {
//a class holding product objects for managing through app
public void setProductName(String productName) {
this.productName = productName;
}
private String productName;
private String productPrice;
private String oldPrice;
private int attrCount;
HashMap<String, ArrayList<String>> attrs;
private String description;
private boolean isLoading = false;
private boolean isNew = false;
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
private String imageUrl="";
public Product( String productName, String productPrice, boolean isNew,String imageUrl) {
this.productName = productName;
this.productPrice = productPrice;
this.isNew = isNew;
this.imageUrl=imageUrl;
}
public Product() {
}
public boolean isNew() {
return isNew;
}
public void setProductPrice(String productPrice) {
this.productPrice = productPrice;
}
public String getProductName() {
return productName;
}
But for managing attributes I need a way to bind each attribute name with its options to keep track of them in future.
Because the attributes and options coming from the server are varied each time I can't use something like enums.
I tried to store the data using HashMap<String,ArrayList> in my products class which takes the attribute as a key and option arrays as values.
HashMap<String,ArrayList<String>>attrs=new HashMap<>();
for (int j=0;j<attrJsonArray.length();j++){
JSONArray optionJsonArray=new JSONArray(attrJsonArray.getJSONObject(j).getString("options"));
ArrayList<String>attrsOptionArray= new ArrayList<>();
for(int k=0;k<optionJsonArray.length();k++){
attrsOptionArray.add(optionJsonArray.getString(k));
}
attrs.put(attrJsonArray.getJSONObject(j).getString("name"),attrsOptionArray);
}
but it seems like a bad practice. I wonder what is the right way to store this kind of data.
You can parse json to java class.
1.Use com.fasterxml.jackson.databind.ObjectMapper for parsing json.
com.fasterxml.jackson.databind.ObjectMapper objectMapper = new ObjectMapper();
Product product = objectMapper.readValue(dataOfJson, Product.class);
2.Make java class for json.
Key of json is field name or name of #JsonProperty.
Array of json is List or array.
If exist The deeper field like "attributes",Use nested static class.
class Product {
#JsonProperty("name")
private String productName;
private String id;
... other field
private List<Attributes> attributes;
// set and get method
static class Attributes{
private String id;
private List<String> options;
... other field
//set and get method
}
}
If you want to try testing, Use this. But I changed a little your json because Your json is not completed.
public class JsonToPojo {
public static void main(String[] args) throws JsonMappingException, JsonProcessingException {
String dataOfJson = " {\r\n"
+ " \"id\": 1860,\r\n"
+ " \"name\": \"T-Shirt\",\r\n"
+ "\r\n"
+ " \"attributes\": [{\r\n"
+ " \"id\": 1,\r\n"
+ " \"name\": \"color\",\r\n"
+ " \"position\": 0,\r\n"
+ " \"visible\": true,\r\n"
+ " \"variation\": false,\r\n"
+ " \"options\": [\r\n"
+ " \"blue\",\r\n"
+ " \"green\",\r\n"
+ " \"red\"\r\n"
+ " ]\r\n"
+ " },\r\n"
+ "\r\n"
+ " {\r\n"
+ " \"id\": 2,\r\n"
+ " \"name\": \"size\",\r\n"
+ " \"position\": 3,\r\n"
+ " \"visible\": true,\r\n"
+ " \"variation\": false,\r\n"
+ " \"options\": [\r\n"
+ " \"L\",\r\n"
+ " \"M\",\r\n"
+ " \"XL\",\r\n"
+ " \"XXL\"\r\n"
+ " ]\r\n"
+ " }\r\n"
+ " ]\r\n"
+ "}";
com.fasterxml.jackson.databind.ObjectMapper objectMapper = new ObjectMapper();
Product product = objectMapper.readValue(dataOfJson, Product.class);
System.out.println(product);
}
}
class Product {
#JsonProperty("name")
private String productName;
private String id;
private List<Attributes> attributes;
static class Attributes{
private String id;
private String name;
private int position;
private boolean visible;
private boolean variation;
private List<String> options;
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 int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
public boolean isVisible() {
return visible;
}
public void setVisible(boolean visible) {
this.visible = visible;
}
public boolean isVariation() {
return variation;
}
public void setVariation(boolean variation) {
this.variation = variation;
}
public List<String> getOptions() {
return options;
}
public void setOptions(List<String> options) {
this.options = options;
}
#Override
public String toString() {
return "Attributes [id=" + id + ", name=" + name + ", position=" + position + ", visible=" + visible
+ ", variation=" + variation + ", options=" + options + "]";
}
}
public String getProductName() {
return productName;
}
public void setProductName(String productName) {
this.productName = productName;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public List<Attributes> getAttributes() {
return attributes;
}
public void setAttributes(List<Attributes> attributes) {
this.attributes = attributes;
}
#Override
public String toString() { // for printing output
return "Product [productName=" + productName + ", id=" + id + ", attributes=" + attributes + "]";
}
}
I have this extremely long JSON file that has a structure like this
{
"count":123456,
"tags":[
{
"sameAs":["https://www.wikidata.org/wiki/Q11254"],
"url":"https://world.openfoodfacts.org/ingredient/salt",
"products":214841,
"name":"Salt",
"id":"en:salt"
},
{
"url":"https://world.openfoodfacts.org/ingredient/sugar",
"sameAs":["https://www.wikidata.org/wiki/Q11002"],
"name":"Sugar",
"id":"en:sugar",
"products":184348
},
...
]
The order of the inner tag objects do not remain the same but i dont think that would pose a problem. Currently this is the code that im using to parse this JSON Object:
This is the container holding the count item as well as the list of tags called IngredientItem.
public class Ingredients {
private int count;
private List<IngredientItem> items;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public List<IngredientItem> getItems() {
return items;
}
public void setItems(List<IngredientItem> items) {
this.items = items;
}
}
This is the code for each tag:
public class IngredientItem {
private List<String> sameAs;
private String id;
private String name;
private String url;
private int productNumber;
public IngredientItem(List<String> sameAs, String id, String name, String url, int productNumber) {
this.sameAs = sameAs;
this.id = id;
this.name = name;
this.url = url;
this.productNumber = productNumber;
}
public List<String> getSameAs() {
return sameAs;
}
public void setSameAs(List<String> sameAs) {
this.sameAs = sameAs;
}
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 getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public int getProductNumber() {
return productNumber;
}
public void setProductNumber(int productNumber) {
this.productNumber = productNumber;
}
#Override
public String toString() {
return "product number: " + getProductNumber() +
"\n" + "name: " + getName() +
"\n" + "id: " + getId() +
"\n" + "same as: " + getSameAs() +
"\n" + "url: " + getUrl();
}
}
and this is my main code to actually parse it.
Gson gson = new Gson();
FileReader fr = new FileReader("path\\to\\file\\ingredients.json");
Ingredients ingredients = gson.fromJson(fr,Ingredients.class);
if(ingredients.getItems() ==null){
System.out.println("NULL");
}else{
for (IngredientItem item: ingredients.getItems()) {
System.out.println(item.toString());
}
}
for some reason it wont ever fill up the items from all the tags. I have already extensively looked at this Parsing a complex Json Object using GSON in Java question and I cannot seem to find the error. The link to downloading this extremely long JSON file is here Really Long JSON File. If you save the page as a .json it is around 121MB so just keep that noted.
Thank you in advance. If any other information is required please let m
For it to be automatic, you need to change Ingredients.items to Ingredients.tags.
If you want to keep your object structure, you can check here how to do it with a Custom Deserializer or Annotations.
i really ned help with this. Im not being able to read the JSON and i dont know what im doing wrong.
I will drop my code here.
I have this Json
{
"id": "288",
"name": "Tarjeta Shopping",
"secure_thumbnail": "https://www.mercadopago.com/org-img/MP3/API/logos/288.gif",
"thumbnail": "http://img.mlstatic.com/org-img/MP3/API/logos/288.gif",
"processing_mode": "aggregator",
"merchant_account_id": null
}
This is my class that should represent that JSON
public class Tarjeta {
#SerializedName("id")
#Expose
private String id;
#SerializedName("name")
#Expose
private String name;
#SerializedName("secure_thumbnail")
#Expose
private String secureThumbnail;
#SerializedName("thumbnail")
#Expose
private String thumbnail;
#SerializedName("processing_mode")
#Expose
private String processingMode;
#SerializedName("merchant_account_id")
#Expose
private Object merchantAccountId;
public Tarjeta() {
}
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 getSecureThumbnail() {
return secureThumbnail;
}
public void setSecureThumbnail(String secureThumbnail) {
this.secureThumbnail = secureThumbnail;
}
public String getThumbnail() {
return thumbnail;
}
public void setThumbnail(String thumbnail) {
this.thumbnail = thumbnail;
}
public String getProcessingMode() {
return processingMode;
}
public void setProcessingMode(String processingMode) {
this.processingMode = processingMode;
}
public Object getMerchantAccountId() {
return merchantAccountId;
}
public void setMerchantAccountId(Object merchantAccountId) {
this.merchantAccountId = merchantAccountId;
}
#Override
public String toString() {
return "Tarjeta{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
", secureThumbnail='" + secureThumbnail + '\'' +
", thumbnail='" + thumbnail + '\'' +
", processingMode='" + processingMode + '\'' +
", merchantAccountId=" + merchantAccountId +
'}';
}
}
this is my GET method
#GET("payment_methods/card_issuers")
Call<Tarjeta> getTarjetas2(#Query("public_key") String apiKey,
#Query("payment_method_id") String payment_method_id);
And this is where i try to read it.
botonTest2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("Test boton 2 clickeado");
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
ServicePago servicePago = retrofit.create(ServicePago.class);
Call<Tarjeta> contenedorTarjetaCall = servicePago.getTarjetas2(apiKey,"visa");
contenedorTarjetaCall.enqueue(new Callback<Tarjeta>() {
#Override
public void onResponse(Call<Tarjeta> call, Response<Tarjeta> response) {
Toast.makeText(MainActivity.this, "BIEN", Toast.LENGTH_SHORT).show();
}
#Override
public void onFailure(Call<Tarjeta> call, Throwable t) {
Toast.makeText(MainActivity.this, "ALGO SALIO MAL", Toast.LENGTH_SHORT).show();
}
});
}
});
Im habing this error: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was BEGIN_ARRAY at line 1 column 2 path $
I think my class is correctly modelated, im completly lost.
since you did not post your entire JSON I'm going to answer with a rough idea hope it helps.
the error states that the received JSON is not a Tarjeta but an array of Tarjeta. so to fix it I guess you just have to wrap your response in a list type. so it goes something like this:
#GET("payment_methods/card_issuers")
Call<List<Tarjeta>> getTarjetas2(#Query("public_key") String apiKey,
#Query("payment_method_id") String payment_method_id);
I have some input values like this:
customername
phone
email
.....
some items values
itemname1
itemname2
........
In every item name have some unitprice
quantity
........
I'm trying to encode these values in json like this:
{
"info": {
"customername": "abc",
"phone": "123",
"email": "an#gmail.com",
},
"item": {
"itemname1": {
"unitprice": "100",
"qty": "3",
},
"itemname2": {
"unitprice": "500",
"qty": "2",
}
}
}
I 'm unable to encode all value like above.
here is my code:
private void jsonEncoding() throws JSONException {
JSONObject obj = new JSONObject();
JSONObject obj1 = new JSONObject();
JSONObject obj2 = new JSONObject();
try {
obj1.put("name", name);
obj1.put("email", email);
obj1.put("phone", phone);
} catch (JSONException e) {
e.printStackTrace();
}
obj.put("info",obj1 );
System.out.print(obj);
System.out.print(obj1);
}
How to encode values like above json format.
You can do it in 2 ways:
Using 3rd party libraries like Gson/Jackson for converting the POJO class data to json object string (use http://www.jsonschema2pojo.org/ for creating POJO class easily)
Manually do JsonParsing like below:
try {
// info node
JSONObject objInfo = new JSONObject();
objInfo.put("name", name);
objInfo.put("email", email);
objInfo.put("phone", phone);
// itemname1 node
JSONObject itemname1 = new JSONObject();
itemname1.put("unitprice", unitprice1);
itemname1.put("qty", qty1);
// itemname2 node
JSONObject itemname2 = new JSONObject();
itemname2.put("unitprice", unitprice2);
itemname2.put("qty", qty2);
// item node
JSONObject item = new JSONObject();
// adding itemname1 & itemname2 to item
item.put("itemname1",itemname1);
item.put("itemname2",itemname2);
// root node
JSONObject root = new JSONObject();
root.put("info",objInfo);
root.put("item",item);
System.out.print(root);
} catch(JsonException e){
e.printStackTrace();
}
Create a POJO class similar to your response
class Info {
String customerName;
String phone;
String email;
}
class ItemName{
String unitprice;
String qty;
}
class Item{
ItemName itemname1;
ItemName itemname2;
}
class Data{
Info info;
Item item;
}
Now just use google gson library to convert instance of Data to json as in
new Gson().toJson(data);
Try this it may help
You need to create three classes and as your using Gson means you are going with networking feature hence implement serializable for all like below.
class CustomObject implements Serializable {
Info info;
Map<String, ItemName> item;
public CustomObject() {
this(null, null);
}
public CustomObject(Info info, Map<String, ItemName> item) {
this.info = info;
this.item = item;
}
public Info getInfo() {
return info;
}
public void setInfo(Info info) {
this.info = info;
}
public Map<String, ItemName> getItem() {
return item;
}
public void setItem(Map<String, ItemName> item) {
this.item = item;
}
}
class Info implements Serializable {
String customername;
String phone;
String email;
public Info() {
this("","","");
}
public Info(String customername, String phone, String email) {
this.customername = customername;
this.phone = phone;
this.email = email;
}
public String getCustomername() {
return customername;
}
public void setCustomername(String customername) {
this.customername = customername;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
class ItemName implements Serializable {
String unitprice;
String qty;
public ItemName() {
this("", "");
}
public ItemName(String unitprice, String qty) {
this.unitprice = unitprice;
this.qty = qty;
}
public String getUnitprice() {
return unitprice;
}
public void setUnitprice(String unitprice) {
this.unitprice = unitprice;
}
public String getQty() {
return qty;
}
public void setQty(String qty) {
this.qty = qty;
}
}
Now You Have to add your data as per need like below.
CustomObject customObject = new CustomObject();
Info info = new Info();
info.setCustomername("abc");
info.setPhone("123");
info.setEmail("an#gmail.com");
customObject.setInfo(info);
ItemName itemname1 = new ItemName();
itemname1.setUnitprice("100");
itemname1.setQty("3");
ItemName itemname2 = new ItemName();
itemname2.setUnitprice("500");
itemname2.setQty("2");
Map<String, ItemName> item = new HashMap<>();
item.put("itemname1", itemname1);
item.put("itemname2", itemname2);
customObject.setItem(item);
System.out.println(new Gson().toJson(customObject));
Above System.out.println gives output as
{
"info":
{
"customername":"abc",
"phone":"123",
"email":"an#gmail.com"},
"item":{
"itemname1":
{
"unitprice":"100",
"qty":"3"
},
"itemname2":
{
"unitprice":"500",
"qty":"2"
}
}
}
Now how to parse your string which you got from server or any source to that CustomObject class. Its very simple through Gson library by google.
String str = "{\n" +
" \"info\":\n" +
" {\n" +
" \"customername\":\"abc\",\n" +
" \"phone\":\"123\",\n" +
" \"email\":\"an#gmail.com\"\n" +
" },\n" +
" \"item\": \n" +
" {\n" +
" \"itemname1\":\n" +
" {\n" +
" \"unitprice\":\"100\",\n" +
" \"qty\":\"3\"\n" +
" },\n" +
" \"itemname2\":\n" +
" {\n" +
" \"unitprice\":\"500\",\n" +
" \"qty\":\"2\"\n" +
" }\n" +
" }\n" +
" }";
CustomObject customObject = new Gson().fromJson(str, new TypeToken<CustomObject>(){}.getType());
System.out.println(new Gson().toJson(customObject));
Class contains all the songs
public class Songs{
private List levels;
public List getLevels() {
return levels;
}
public void setLevels(List levels) {
this.levels = levels;
}
}
each song object
public class Levels{
private Number id;
private String name;
private List sequence;
public Number getId(){
return this.id;
}
public void setId(Number id){
this.id = id;
}
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
public List getSequence() {
return sequence;
}
public void setSequence(List sequence) {
this.sequence = sequence;
}
}
JSON
{
"levels": [
{
"id": 1,
"name": "Sequence",
"sequence": [
17,
1,
2
]
},
{
"id": 2,
"name": "Sequence",
"sequence": [
17,
0,
1,
2,
4,
4,
5,
6
]
}
]
}
Java code
This works if I debug I can see the objects but the problem is getting sequence the array of int. Can Anyone help me ??? I can paste the stack trace if I do list2.getLevels().get(0).getSequence();
String json = new String(b);
Gson gson = new Gson();
Songs list2 = (Songs) gson.fromJson(json, Songs.class);
//I CAN READ ONE LEVEL LIKE THIS
LinkedTreeMap<String,Levels> l = (LinkedTreeMap)songs.getLevels().get(0);
//HOW CAN I GET SEQUENCE ARRAY???
Levels.java
import java.util.List;
public class Levels {
private Number id;
private String name;
private List sequence;
public Number getId(){
return this.id;
}
public void setId(Number id){
this.id = id;
}
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
public List getSequence() {
return sequence;
}
public void setSequence(List sequence) {
this.sequence = sequence;
}
}
Songs.java
import java.util.List;
public class Songs{
private List<Levels> levels;
public List<Levels> getLevels() {
return levels;
}
public void setLevels(List<Levels> levels) {
this.levels = levels;
}
}
and use this to get the sequence list:
String json = new String("{\n \"levels\": [\n {\n \"id\": 1,\n \"name\": \"Sequence\",\n \"sequence\": [\n 17,\n 1,\n 2\n ]\n },\n {\n \"id\": 2,\n \"name\": \"Sequence\",\n \"sequence\": [\n 17,\n 0,\n 1,\n 2,\n 4,\n 4,\n 5,\n 6\n ]\n }\n ]\n}");
Gson g = new Gson();
Songs vc = (Songs)g.fromJson(json, Songs.class);
List test = vc.getLevels().get(0).getSequence();
I'm going to paste a sample that I created but stored in shared preferences.
SharedPreferences settings = getSharedPreferences(String name, MODE_MULTI_PROCESS);
SharedPreferences.Editor editor = settings.edit();
Gson gson = new Gson();
String json = gson.toJson(ArrayList<Object>);
editor.putString("list", json);
editor.commit();
And then when I want to read this Json I just do:
SharedPreferences settings = getSharedPreferences(String name, MODE_MULTI_PROCESS);
String shared = settings.getString("list", null);
Gson gson = new Gson();
ArrayList<Object> temp = gson.fromJson(shared, new TypeToken<ArrayList<Object>>(){}.getType());