Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何获取json的索引并在pojo类中使用它_Java_Json_Retrofit_Pojo - Fatal编程技术网

Java 如何获取json的索引并在pojo类中使用它

Java 如何获取json的索引并在pojo类中使用它,java,json,retrofit,pojo,Java,Json,Retrofit,Pojo,我有一个类似这样的JSON。 我用改装来获得它。 第一个索引是来自网站的时间,每4小时更新一次 这是我的json: 如您所见,时间索引是可变的(可变的) 如何为这个JSON创建POJO类?我建议您将这个JSON解析为一个Map,其中SomeExampleClass是一个POJO,您创建它来表示与每个日期关联的对象 例如,我在上使用了JSON的内部部分(我选择gson作为注释样式)并生成了POJO类 以下是JSON的一部分: { "donations": 4070, "member

我有一个类似这样的JSON。 我用改装来获得它。 第一个索引是来自网站的时间,每4小时更新一次

这是我的json:

如您所见,时间索引是可变的(可变的)


如何为这个JSON创建POJO类?

我建议您将这个JSON解析为一个
Map
,其中
SomeExampleClass
是一个POJO,您创建它来表示与每个日期关联的对象

例如,我在上使用了JSON的内部部分(我选择gson作为注释样式)并生成了POJO类

以下是JSON的一部分:

{
    "donations": 4070,
    "memberCount": 50,
    "members": [
        {
            "clanRank": 1,
            "crowns": 0,
            "donations": 58,
            "name": "Min",
            "tag": "L88P2282",
            "trophies": 4610
        },
        {
            "clanRank": 2,
            "crowns": 0,
            "donations": 76,
            "name": "matiz",
            "tag": "G0JVYY0",
            "trophies": 4443
        }
    ]
}
下面是生成的类,我通过Eclipse添加了
toString
方法:

package com.example;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Example {

    @SerializedName("donations")
    @Expose
    private Integer donations;
    @SerializedName("memberCount")
    @Expose
    private Integer memberCount;
    @SerializedName("members")
    @Expose
    private List<Member> members = null;

    public Integer getDonations() {
        return donations;
    }

    public void setDonations(Integer donations) {
        this.donations = donations;
    }

    public Integer getMemberCount() {
        return memberCount;
    }

    public void setMemberCount(Integer memberCount) {
        this.memberCount = memberCount;
    }

    public List<Member> getMembers() {
        return members;
    }

    public void setMembers(List<Member> members) {
        this.members = members;
    }

    @Override
    public String toString() {
        return "Example [donations=" + donations + ", memberCount=" + memberCount + ", members=" + members + "]";
    }

}
下面是一些将JSON解析为
映射的示例代码:

package gson;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;

import com.example.Example;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class GsonMain {

    public static void main(String[] args) throws IOException {
        Gson gson = new Gson();
        byte[] jsonBytes = Files.readAllBytes(Paths.get("./src/main/java/gson/jsonData.json"));
        String json = new String(jsonBytes);

        Map<String,Example> result = gson.fromJson(json, new TypeToken<Map<String, Example>>(){}.getType());

        System.out.println("RESULTS: "+result);
    }

}
结果是以下输出:

结果:{2018-01-01-20-00=示例[捐赠=4070,会员人数=50, 成员=[Member[clanRank=1,皇冠=0,捐款=58,姓名=Min, tag=L88P2282,奖杯=4610],成员[clanRank=2,王冠=0, 捐款=76,姓名=matiz,标签=G0JVYY0,奖杯=4443]], 2018-01-02-00-00=示例[捐款=null,会员人数=null, 成员=null]}


然后,你可以根据需要在
地图中

学习谷歌,这是一项惊人的技能,你正在寻找类似的东西。不幸的是,这个网站帮不了我。因为这个网站只是做pojo。但我不想知道如何创建pojo,我知道。我只是想知道如何获取索引并使用它。@ezio您需要创建一个POJO列表,这样您就可以使用
List.get(index)获取该项如果此时变量是动态的,我建议使用JSON数组而不是JSON ObjectsHanks。你的意思是在发送之前更改json更好?谢谢。你向我解释了基本原理,我已经按照你的解决方案做了我想做的事情。完美的如果你知道任何关于地图的培训视频,我很乐意知道。不管是来自youtube还是其他地方。我只想成为专业的地图工作<代码>地图
是其中的一部分,您可能应该从这里开始了解它们。
package com.example;

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

public class Member {

    @SerializedName("clanRank")
    @Expose
    private Integer clanRank;
    @SerializedName("crowns")
    @Expose
    private Integer crowns;
    @SerializedName("donations")
    @Expose
    private Integer donations;
    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("tag")
    @Expose
    private String tag;
    @SerializedName("trophies")
    @Expose
    private Integer trophies;

    public Integer getClanRank() {
        return clanRank;
    }

    public void setClanRank(Integer clanRank) {
        this.clanRank = clanRank;
    }

    public Integer getCrowns() {
        return crowns;
    }

    public void setCrowns(Integer crowns) {
        this.crowns = crowns;
    }

    public Integer getDonations() {
        return donations;
    }

    public void setDonations(Integer donations) {
        this.donations = donations;
    }

    public String getName() {
        return name;
    }

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

    public String getTag() {
        return tag;
    }

    public void setTag(String tag) {
        this.tag = tag;
    }

    public Integer getTrophies() {
        return trophies;
    }

    public void setTrophies(Integer trophies) {
        this.trophies = trophies;
    }

    @Override
    public String toString() {
        return "Member [clanRank=" + clanRank + ", crowns=" + crowns + ", donations=" + donations + ", name=" + name
                + ", tag=" + tag + ", trophies=" + trophies + "]";
    }

}
package gson;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Map;

import com.example.Example;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class GsonMain {

    public static void main(String[] args) throws IOException {
        Gson gson = new Gson();
        byte[] jsonBytes = Files.readAllBytes(Paths.get("./src/main/java/gson/jsonData.json"));
        String json = new String(jsonBytes);

        Map<String,Example> result = gson.fromJson(json, new TypeToken<Map<String, Example>>(){}.getType());

        System.out.println("RESULTS: "+result);
    }

}
{
        "2018-01-01-20-00": {
            "donations": 4070,
            "memberCount": 50,
            "members": [
                {
                    "clanRank": 1,
                    "crowns": 0,
                    "donations": 58,
                    "name": "Min",
                    "tag": "L88P2282",
                    "trophies": 4610
                },
                {
                    "clanRank": 2,
                    "crowns": 0,
                    "donations": 76,
                    "name": "matiz",
                    "tag": "G0JVYY0",
                    "trophies": 4443
                }
            ]
        },
        "2018-01-02-00-00": {
        }
    }