-4

I am using:

Gson gson = new Gson();
return gson.fromJson(jsonstr,cls);

Where jsonstr is:

{"title": "ttl","data": "dta"}

And cls is:

public class GSONTest {

public String title;
public String data;

public String getTitle() {
    return title;
}

public String getData() {
    return data;
}

}

But I with do to something more complicated, here is example of my json:

{
  "Some1": {
    "data": "dta",
    "title": "ttl"
  },
  "Some3": {
    "data": "dta2",
    "title": "ttl2"
  }
}

How should my class look like for this type of json?

Dim
  • 4,527
  • 15
  • 80
  • 139

4 Answers4

0
    Type type = new TypeToken<Map<String, GSONTest>>(){}.getType();
    Map<String, GSONTest> gsonTestMap = gson.fromJson(jsonstr, type);
John O'Reilly
  • 10,000
  • 4
  • 41
  • 63
0

As you can see every JSON Object is like a class in java so you should create a Java class for the root of your JSON something like this:

public class GSONTest {

    GSONTest2 element1;
    GSONTest2 element2;

    //constructor, getters and setters here

}

You see that I used another object so it can stored the "level2" of your JSON this class will be something like this:

public class GSONTest2{
    String element1; //This will stored "dta and dta2"
    String element2; //This will stored "ttl1 and ttl2"
    //constructor, getters and setters here 

}

So when you are calling GSON you should just do:

Gson gson = new Gson();
return gson.fromJson(jsonstr,GSONTest.class);
Brank Victoria
  • 1,447
  • 10
  • 17
-1

You may see the working here in screen shot
Example {

 @SerializedName("Some1")
 @Expose
 private Some1 some1;
 @SerializedName("Some3")
 @Expose
 private Some3 some3;


 public Some1 getSome1() {
  return some1;
 }

 public void setSome1(Some1 some1) {
  this.some1 = some1;
 }


 public Some3 getSome3() {
  return some3;
 }


 public void setSome3(Some3 some3) {
  this.some3 = some3;
 }

}

-----------------------------------com.example.Some1.java----------------

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

 public class Some1 {

  @SerializedName("data")
  @Expose
  private String data;
  @SerializedName("title")
  @Expose
  private String title;

 public String getData() {
   return data;
 }


 public void setData(String data) {
  this.data = data;
 }


 public String getTitle() {
   return title;
 }


 public void setTitle(String title) {
   this.title = title;
 }

}

-----------------------------------com.example.Some3.java----------------

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

public class Some3 {

@SerializedName("data")
@Expose
private String data;
@SerializedName("title")
@Expose
private String title; 

public String getData() {
return data;
}


public void setData(String data) {
 this.data = data;
}


public String getTitle() {
 return title;
}


public void setTitle(String title) {
 this.title = title;
}

} 

This is activity code for you, ENJOY!!!

 public class MainActivity extends AppCompatActivity {

 String json = "{\n" +
        "  \"Some1\": {\n" +
        "    \"data\": \"dta\",\n" +
        "    \"title\": \"ttl\"\n" +
        "  },\n" +
        "  \"Some3\": {\n" +
        "    \"data\": \"dta2\",\n" +
        "    \"title\": \"ttl2\"\n" +
        "  }\n" +
        "}";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Example mExample = new Gson().fromJson(json, Example.class);

    Toast.makeText(this, mExample.toString(), Toast.LENGTH_SHORT).show();
}
}

You can also use this website to generate class link

Syed Raza Mehdi
  • 4,067
  • 1
  • 31
  • 47
-1

Your class is like this

package com.example;

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

public class Example {

@SerializedName("Some1")
@Expose
private Some1 some1;
@SerializedName("Some3")
@Expose
private Some3 some3;


public Some1 getSome1() {
return some1;
}

public void setSome1(Some1 some1) {
this.some1 = some1;
}

public Some3 getSome3() {
return some3;
}

public void setSome3(Some3 some3) {
this.some3 = some3;
}}

Next Some1.class

package com.example;

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

public class Some1 {

@SerializedName("data")
@Expose
private String data;
@SerializedName("title")
@Expose
private String title;


public String getData() {
return data;
}

public void setData(String data) {
this.data = data;
}


public String getTitle() {
return title;
}


public void setTitle(String title) {
this.title = title;
}

}

next Some3.class

package com.example;

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

public class Some3 {

@SerializedName("data")
@Expose
private String data;
@SerializedName("title")
@Expose
private String title;

/**
* 
* @return
* The data
*/
public String getData() {
return data;
}

/**
* 
* @param data
* The data
*/
public void setData(String data) {
this.data = data;
}

/**
* 
* @return
* The title
*/
public String getTitle() {
return title;
}

/**
* 
* @param title
* The title
*/
public void setTitle(String title) {
this.title = title;
}

}
Nithinlal
  • 4,845
  • 1
  • 29
  • 40