N
N
Nodir Malikov2021-03-03 19:41:44
Java
Nodir Malikov, 2021-03-03 19:41:44

GSON - How to declare variables to store names from complex JSON?

There is this JSON:

[
  {
    "id": 1,
    "name": "Leanne Graham",
    "username": "Bret",
    "email": "[email protected]",
    "address": {
      "street": "Kulas Light",
      "suite": "Apt. 556",
      "city": "Gwenborough",
      "zipcode": "92998-3874",
      "geo": {
        "lat": "-37.3159",
        "lng": "81.1496"
      }
    },
    "phone": "1-770-736-8031 x56442",
    "website": "hildegard.org",
    "company": {
      "name": "Romaguera-Crona",
      "catchPhrase": "Multi-layered client-server neural-net",
      "bs": "harness real-time e-markets"
    }
  },
  {
    "id": 2,
    "name": "Ervin Howell",
    "username": "Antonette",
    "email": "[email protected]",
    "address": {
      "street": "Victor Plains",
      "suite": "Suite 879",
      "city": "Wisokyburgh",
      "zipcode": "90566-7771",
      "geo": {
        "lat": "-43.9509",
        "lng": "-34.4618"
      }
    },
    "phone": "010-692-6593 x09125",
    "website": "anastasia.net",
    "company": {
      "name": "Deckow-Crist",
      "catchPhrase": "Proactive didactic contingency",
      "bs": "synergize scalable supply-chains"
    }
  },
]


I am working with the GSON library. The program needs to find geodata by mail.
How to declare variables in the GetGeo class for further work with it.
Namely, I don't understand how to declare the key "address" and "geo" inside it.
GetGeo

public class GetGeo {
    private int id;
    private String name;
    private String username;
    private String email;
    private Object address;
}


PS In the task it is specified to use GSON.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
O
Orkhan, 2021-03-03
@Fayo

There is such a site - www.jsonschema2pojo.org
On the site you will also find a maven plugin for quick conversion

Result

-----------------------------------com.example.Address.java-----------------------------------

package com.example;

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

public class Address {

@SerializedName("street")
@Expose
public String street;
@SerializedName("suite")
@Expose
public String suite;
@SerializedName("city")
@Expose
public String city;
@SerializedName("zipcode")
@Expose
public String zipcode;
@SerializedName("geo")
@Expose
public Geo geo;

}
-----------------------------------com.example.Company.java-----------------------------------

package com.example;

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

public class Company {

@SerializedName("name")
@Expose
public String name;
@SerializedName("catchPhrase")
@Expose
public String catchPhrase;
@SerializedName("bs")
@Expose
public String bs;

}
-----------------------------------com.example.Example.java-----------------------------------

package com.example;

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

public class Example {

@SerializedName("id")
@Expose
public int id;
@SerializedName("name")
@Expose
public String name;
@SerializedName("username")
@Expose
public String username;
@SerializedName("email")
@Expose
public String email;
@SerializedName("address")
@Expose
public Address address;
@SerializedName("phone")
@Expose
public String phone;
@SerializedName("website")
@Expose
public String website;
@SerializedName("company")
@Expose
public Company company;

}
-----------------------------------com.example.Geo.java-----------------------------------

package com.example;

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

public class Geo {

@SerializedName("lat")
@Expose
public String lat;
@SerializedName("lng")
@Expose
public String lng;

}

S
Stepan, 2021-03-03
@steff

Maybe this article will help: https://habr.com/ru/company/naumen/blog/228279/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question