【问题标题】:Is it possible to declare a string / int as numer?是否可以将字符串 / int 声明为数字?
【发布时间】:2019-05-30 17:50:55
【问题描述】:

我想用 Java 解析 JSON,但我的 JSON 看起来像这样:

...
{
    "total_count": 16,
    "entries": [
        {
            "2": "1788",
            "3": "Yes", 
            "id": "2009131"
         },
         {
            "2": "956",
            "3": "No", 
            "id": "1381"
         }
...

我已经想出通过以下方式“提取”条目:

if(jsonTree.isJsonObject()){
            System.out.println("True");
            JsonObject jsonObject = jsonTree.getAsJsonObject();

            JsonElement f2 = jsonObject.get("entries");

现在我有一个 JSON 数组。为了用 gson 进行解析,我需要一个带有这样变量的类(至少我是这么认为的):

int 2;
String 3;

据我所知,这是不可声明的。

有没有办法(前缀,语法?)来实现?或者另一种方法来提取和分配具有正确 id 的值?

【问题讨论】:

标签: java json string gson declaration


【解决方案1】:

您可以使用SerializedName 注释,它允许在Java 文件中定义与属性名称不同的字段名称。下面您可以看到如何将您的 JSON 有效负载反序列化为 Java 模型的示例:

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.SerializedName;

import java.io.File;
import java.io.FileReader;
import java.util.List;

public class GsonApp {

    public static void main(String[] args) throws Exception {
        File jsonFile = new File("./resource/test.json").getAbsoluteFile();

        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        Root root = gson.fromJson(new FileReader(jsonFile), Root.class);
        System.out.println(root);
    }
}

class Root {

    @SerializedName("total_count")
    private int totalCount;
    private List<Entry> entries;

    // getters, setters, toString
}

class Entry {
    private String id;

    @SerializedName("2")
    private String two;

    @SerializedName("3")
    private String three;

    // getters, setters, toString
}

上面的代码打印:

Root{totalCount=16, entries=[Entry{id='2009131', two='1788', three='Yes'}, Entry{id='1381', two='956', three='No'}]}

如果您的 JSON 是动态的并且可以拥有更多/更少的密钥,您可以将其反序列化为 Map&lt;String, String&gt;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.annotations.SerializedName;

import java.io.File;
import java.io.FileReader;
import java.util.List;
import java.util.Map;

public class GsonApp {

    public static void main(String[] args) throws Exception {
        File jsonFile = new File("./resource/test.json").getAbsoluteFile();

        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        Root root = gson.fromJson(new FileReader(jsonFile), Root.class);
        System.out.println(root);
    }
}

class Root {

    @SerializedName("total_count")
    private int totalCount;
    private List<Map<String, String>> entries;

    // getters, setters, toString
}

上面的代码打印:

Root{totalCount=16, entries=[{2=1788, 3=Yes, id=2009131}, {2=956, 3=No, id=1381}]}

【讨论】:

    【解决方案2】:

    您可以创建一个对象“条目”并在其中包含所有可能的变量。有 JSON 库可以为您进行转换。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-09
      相关资源
      最近更新 更多