简体   繁体   English

如何使用GSON在对象中插入许多JSON属性

[英]How can I insert many JSON Attributes in a Object using GSON

I'm trying to parse a JSON ( http://economia.awesomeapi.com.br/json/all ) response, I Don't know why when I use the Gson().fromJson() my object is not been populated. 我正在尝试解析JSON( http://economia.awesomeapi.com.br/json/all )响应,我不知道为什么当我使用Gson()。fromJson()时未填充我的对象。

GetCoinsBalanceJson Method GetCoinsBalanceJson方法

    private void getCoinsBalanceJson() {
    String httpURL = "http://economia.awesomeapi.com.br/json/all";
    URL myUrl;
    StringBuilder coinData = new StringBuilder();
    CoinJsonWrapper coinJsonWrapper = new CoinJsonWrapper();

    try {
        myUrl = new URL(httpURL);
        HttpURLConnection conn = (HttpURLConnection) myUrl.openConnection();
        conn.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");
        InputStream is = conn.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);

        String inputLine;

        while ((inputLine = br.readLine()) != null) {
            coinData.append(inputLine);

        }
        Gson g = new Gson();

        //CoinJsonWrapper[] enums = g.fromJson(coinData, CoinJsonWrapper[].class);
        coinJsonWrapper = g.fromJson(coinData.toString(), CoinJsonWrapper.class);    // This Object is not been Populated :(

    } catch (Exception e) {
        System.out.println("Exception is " + e.getMessage());
    }
}

Class CoinJsonWrapper CoinJsonWrapper类

    public class CoinJsonWrapper {

    @SerializedName("code")
    private String code;

    @SerializedName("codein")
    private String codein;

    @SerializedName("name")
    private String name;

    @SerializedName("high")
    private Float high;

    @SerializedName("low")
    private Float low;

    @SerializedName("ask")
    private Float ask;

    @SerializedName("timestamp")
    private Long timestamp;

    @SerializedName("create_date")
    private String create_date;

    public final String get_code() {
        return this.code;
    }

    public final String get_codein() {
        return this.codein;
    }

    public final String get_name() {
        return this.code;
    }

    public final Float get_high() {
        return this.high;
    }

    public final Float get_low() {
        return this.low;
    }

    public final Float get_ask() {
        return this.ask;
    }

    public final Long get_timestamp() {
        return this.timestamp;
    }

     public final String get_create_date() {
        return this.create_date;
    }


}

I dont know why the my object --> coinJsonWrapper is not been populated with the values os Json Response. 我不知道为什么我的对象-> coinJsonWrapper没有用os Json Response值填充。 Can someone help me with this probleam? 有人可以帮我解决这个问题吗?

Tks! Tks!

You didn't define your model class correctly, they should look like this: 您没有正确定义模型类,它们应如下所示:

public class Coin {

    @SerializedName("code")
    private String code;

    @SerializedName("codein")
    private String codein;

    @SerializedName("name")
    private String name;

    @SerializedName("high")
    private Float high;

    @SerializedName("low")
    private Float low;

    @SerializedName("ask")
    private Float ask;

    @SerializedName("timestamp")
    private Long timestamp;

    @SerializedName("create_date")
    private String create_date;

    public final String get_code() {
        return this.code;
    }

    public final String get_codein() {
        return this.codein;
    }

    public final String get_name() {
        return this.code;
    }

    public final Float get_high() {
        return this.high;
    }

    public final Float get_low() {
        return this.low;
    }

    public final Float get_ask() {
        return this.ask;
    }

    public final Long get_timestamp() {
        return this.timestamp;
    }

    public final String get_create_date() {
        return this.create_date;
    }
}

And

public class CoinsWrapper {

    @SerializedName("USDT")
    private Coin usdt;

    @SerializedName("USD")
    private Coin usd;

    @SerializedName("CAD")
    private Coin cad;

    @SerializedName("EUR")
    private Coin eur;

    @SerializedName("GBP")
    private Coin gbp;

    @SerializedName("ARS")
    private Coin ars;

    @SerializedName("BTC")
    private Coin btc;

    public final Coin get_usdt() {
        return this.usdt;
    }

    public final Coin get_usd() {
        return this.usd;
    }

    public final Coin get_cad() {
        return this.cad;
    }

    public final Coin get_eur() {
        return this.eur;
    }

    public final Coin get_gbp() {
        return this.gbp;
    }

    public final Coin get_ars() {
        return this.ars;
    }

    public final Coin get_btc() {
        return this.btc;
    }
}

And in your method getCoinsBalanceJson: 并在您的方法getCoinsBalanceJson中:

coinJsonWrapper = g.fromJson(coinData.toString(), CoinsWrapper.class);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当JSON使用日期作为属性名称时,如何使用GSON将JSON反序列化为Java Object? - How can I deserialize JSON to Java Object using GSON when the JSON using dates as property names? 如何使用 GSON 在 JSON 响应中动态查找对象数组的结尾? - How can I dynamically find the end of an object array inside of a JSON response using GSON? 如何通过Gson将以下JSON字符串转换为Java Object? - How can I convert the following JSON string to Java Object by Gson? 如何使用 Z6716608096280862A88268CDDB0DEZ 访问以 NULL 开头的 JSON object? - How do I access a JSON object that begins with NULL using gson? Gson:如何使用gson将json对象隐蔽到子类对象 - Gson : How to covert json object to child class object using gson 使用Gson如何转换Json对象,该对象包含由许多类实现的接口的对象-Java - Using Gson How to convert Json object that contains object of an interface that implemented by many classes - Java 如何在没有敏感属性的情况下通过 Gson 打印 JSON 对象? - How to print JSON object by Gson without sensitive attributes? 如何使用 Java PreparedStatement 将 JSON 对象插入 Postgres? - How can I Insert JSON object into Postgres using Java preparedStatement? 如何使用gson.fromjson递归转换json - How can I cast json recursively using gson.fromjson 如何使用Retrofit和GSON解析JSON数组? - How can I parse JSON Array Using Retrofit and GSON?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM