java - 无法在 java 中使用 Google Gson 解析此内容

标签 java json

{
    "status":1,
    "list":
        {
            "218888771":
                {
                    "item_id":"218888771",
                    "title":"twitter",
                    "url":"http:\/\/t.co\/oFYGY7z0",
                    "time_updated":"1347094862",
                    "time_added":"1347094862",
                    "state":"0"
                },
            "217345740":
                {
                    "item_id":"217345740",
                    "title":"",
                    "url":"http:\/\/t.co\/dCvNwtrK",
                    "time_updated":"1346790837",
                    "time_added":"1346790700",
                    "state":"0"
                }
        },
    "since":1347094862,
    "complete":1
}

我正在使用 Google GSon,但没有真正取得任何进展。

首先,我很困惑为什么我无法将其转换为 JsonArray。 “list”看起来像一个 json 数组。但这似乎不起作用。我不介意使用 POJO 方法来完成此操作,但是我首先如何解析这个 JSONArray 呢?

  JsonParser parser = new JsonParser();
    JsonElement tradeElement = parser.parse(response);
    JsonObject trade = tradeElement.getAsJsonObject();
    json.get("list").getAsJsonObject().get("url")

这给了我空。

最佳答案

 {
   "complete":1,
   "list":{
      "217345740":{
         "item_id":"217345740",
         "state":"0",
         "time_added":"1346790700",
         "time_updated":"1346790837",
         "title":"",
         "url":"http://t.co/dCvNwtrK"
      },
      "218888771":{
         "item_id":"218888771",
         "state":"0",
         "time_added":"1347094862",
         "time_updated":"1347094862",
         "title":"twitter",
         "url":"http://t.co/oFYGY7z0"
      }
   },
   "since":1347094862,
   "status":1
}

如您所见,当格式正确时,list 是一个具有两个属性 217345740218888771 的对象。数组用方括号 [] 括起来。这就是为什么您无法将其转换为数组的原因。

你最好的选择是正确使用 gson 及其将 json 解析为 POJO 的能力。 这是完全未经测试的(我不太了解 Gson,而且我不在我的开发机器上),但你会看到这个想法。

public class Item {
    long item_id;
    int state;
    @SerializedName("time_added")
    Date timeAdded;
    @SerializedName("time_updated")
    Date timeUpdated;
    String title;
    String url;

    // getter & setters
}

public class Trade {
    int complete;
    Date since;
    int status;
    Map<Long, Item> list;

    // getter & setters
}

public class Foo {
    public static void main(String[] args) {
        String json = "";

        GsonBuilder builder = new GsonBuilder();
        builder.registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
            public Date deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
                return new Date(json.getAsJsonPrimitive().getAsLong()); 
            } 
        });
        Gson gson = builder.create();
        Trade trade = gson.fromJson(json, Trade.class);
        Map<Long, Item> items = trade.getList();
        System.out.println(items.get(217345740L).getUrl()); // should print http://t.co/dCvNwtrK
    }
}

关于java - 无法在 java 中使用 Google Gson 解析此内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12333487/

相关文章:

json - 执行错误,从 org.apache.hadoop.hive.ql.exec.DDLTask 返回代码 1。 com/mongodb/util/JSON

wcf - 替换WCF默认JSON序列化

java - Gson 中的 Stackoverflow 异常

java - 无法单击 '100' 搜索结果类和 xpath - selenium webdriver java

java - Maven/Spring boot项目-如何跳过@SpringBootTest

Java Arraylist 大小

javascript - JSON.stringify 不应该转义 Unicode 字符吗?

python - 如何在 psycopg2 查询中返回 json?

java - GUI + 多线程支持 + 正则表达式支持。哪种语言? Java/Python/ ruby ?

JavaFX:TableView 消息为空时