-1

I have a application that has stored some data in a JSON object. Example as given below,

Can somebody tell me can we do the same thing on JAVA? is there a way i can store data like this on java? Java objects?

I'm very new to this.

Can you show me the steps of converting the above JSON object and how it will look like in java? Please?

Manu
  • 53
  • 2
  • 3
  • 8
  • you need a json object... maybe a POJO that you can serialize!!! – ΦXocę 웃 Пepeúpa ツ Jun 20 '17 at 12:23
  • https://stackoverflow.com/questions/31638798/convert-json-to-an-object-in-android-with-gson – Pavya Jun 20 '17 at 12:25
  • Here you go https://www.tutorialspoint.com/json/json_java_example.htm – urag Jun 20 '17 at 12:25
  • so you already have a application to store the data in JSON object format. Then what you need again. – Rameshbabu Jun 20 '17 at 12:27
  • Note that Android provide a JSON API, you can find a lot of post about this, like [this one](https://stackoverflow.com/questions/8091051/how-to-parse-json-string-in-android) – AxelH Jun 20 '17 at 12:41
  • Possible duplicate of [How to parse json string in Android?](https://stackoverflow.com/questions/8091051/how-to-parse-json-string-in-android) – AxelH Jun 20 '17 at 12:44

1 Answers1

1

Yes you can use GSON lib.

GSON Dependency include in gradle

compile 'com.google.code.gson:gson:2.8.0'

then in code. POJO

public class Animal {

    public String parentName;
}

public class Cat extends Animal{
    public String subNameC;
}

public class Dog  extends Animal{
    public String subNameD;
}

then suppose you have any json string like this

 String data="  [
        {'subNameC':'I am cat','parentName':'I am animal C'},
        {'subNameD":'I am dog','parentName':'I am animal D'}
        ]";

then create object of GSON

Gson gson = new Gson();

Finally, we've to map from a JSON to a Java object with fromJson():

TypeToken<List<Animal>> token = new TypeToken<List<Animal>>() {};
List<Animal> animals = gson.fromJson(data, token.getType());

DONE

Pojo for your json object is as follows

-----------------------------------com.example.Places.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Places {

@SerializedName("1")
@Expose
private com.example._1 _1;
@SerializedName("2")
@Expose
private com.example._2 _2;

public com.example._1 get1() {
return _1;
}

public void set1(com.example._1 _1) {
this._1 = _1;
}

public com.example._2 get2() {
return _2;
}

public void set2(com.example._2 _2) {
this._2 = _2;
}

}
-----------------------------------com.example._1.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class _1 {

@SerializedName("name")
@Expose
private String name;
@SerializedName("lat")
@Expose
private double lat;
@SerializedName("lon")
@Expose
private double lon;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public double getLat() {
return lat;
}

public void setLat(double lat) {
this.lat = lat;
}

public double getLon() {
return lon;
}

public void setLon(double lon) {
this.lon = lon;
}

}
-----------------------------------com.example._2.java-----------------------------------

package com.example;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class _2 {

@SerializedName("name")
@Expose
private String name;
@SerializedName("lat")
@Expose
private double lat;
@SerializedName("lon")
@Expose
private double lon;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public double getLat() {
return lat;
}

public void setLat(double lat) {
this.lat = lat;
}

public double getLon() {
return lon;
}

public void setLon(double lon) {
this.lon = lon;
}

}

Your JSON string

String data="{
'places': {
    '1': {'name': 'Pettah','lat': 6.93321,'lon': 79.8554},
    '2': {'name': 'Fort Railway Station','lat': 6.93408,'lon': 79.8502}
}
}";
Gson gson = new Gson();
TypeToken<List<Places>> token = new TypeToken<List<Places>>() {};
List<Places> animals = gson.fromJson(data, token.getType());
Mahesh Lad
  • 1,937
  • 1
  • 6
  • 8