vitalii_hrushyn
12/23/2016 - 9:44 PM

Parsing JSON

Parsing JSON

{

  "name": "Name",
"surname": "Surname",
"phones": ["044-256-78-90", "066-123-45-67"],
"sites": ["http://site1.com", "http://site2.com"],

"address": {
"country": "Ukraine",
"city": "Kyiv",
"street": "Antonovycha"
}
}
import com.google.gson.Gson;

import java.io.*;

/**
 * Created by User on 21.12.2016.
 */
public class Main {
    public static void main (String[] args) throws IOException {
        Gson gson = new Gson();
        File file = new File ("card.json");
        BufferedReader bf = new BufferedReader(new FileReader(file));
        Card card =  gson.fromJson(bf, Card.class);
        System.out.println(card);
    }
}

import java.util.Arrays;

/**
 * Created by User on 20.12.2016.
 */
public class Card {

    String name;
    String surname;
    String[] phones;
    String[] sites;
    Address address;

    public Card() {
    }

    @Override
    public String toString() {
        return "name: " + name + System.lineSeparator() +
                "surname: " + surname + System.lineSeparator()+
                "phones: " + Arrays.toString(phones) + System.lineSeparator()+
                "sites: " + Arrays.toString(sites) + System.lineSeparator()+
                "address: " + System.lineSeparator() + address +
                ' ';
    }
}

/**
 * Created by User on 21.12.2016.
 */
public class Address {

    String country;
    String city;
    String street;

    public Address() {
    }

    @Override
    public String toString() {
        return "" +
                "country: " + country + System.lineSeparator()+
                "city: " + city + System.lineSeparator()+
                "street: " + street + System.lineSeparator()+
                ' ';
    }
}