1

json 看起来像这样:

"hour_totals": {
      "382423": {
        "imp": 126,
        "clk": 1,
        "spend": "$0.03",
        "conv": 0,
        "cpm": "$0.22",
        "cpc": "$0.03",
        "ctr": "0.79%",
        "cpa": "$Inf"
      },
      "382424": {
        "imp": 209,
        "clk": 1,
        "spend": "$0.05",
        "conv": 0,
        "cpm": "$0.23",
        "cpc": "$0.05",
        "ctr": "0.48%",
        "cpa": "$Inf"
      }}

我阅读了 20 多个答案,但无法找到如何反序列化这样的结构,请帮助了解课程的外观,因为小时不是固定字符串。

4

1 回答 1

1

要使用 Gson 解析这个 JSON,您需要两个步骤。

  1. 定义这些类:

    public class Total {
    
       Map<String, HourData> hour_totals;
    
       @Override
       public String toString() {
          return "Total [hour_totals=" + hour_totals + "]";
       }
    
    }
    

    HourData在哪里

    public class HourData {
    
       Integer imp;
       Integer clk;
       String spend;
       Integer conv;
       String cpm;
       String cpc;
       String cpa;
    
       @Override
       public String toString() {
          return "HourData [imp=" + imp + ", clk=" + clk + ", spend=" + spend
                + ", conv=" + conv + ", cpm=" + cpm + ", cpc=" + cpc + ", cpa="
                + cpa + "]";
       }
    
     }
    
  2. 破解您的“Json 字符串”,因为它不是有效的 Json(请参阅下面的更多详细信息)。您只需要像以下代码一样添加大括号:

    public class Q19201300 {
    
      public static void main(String[] args) {
          String json = "\"hour_totals\": {  "
                  + "  \"382423\": {                 "
                  + "  \"imp\": 126,                 "
                  + "  \"clk\": 1,                   "
                  + "  \"spend\": \"$0.03\",         "
                  + "  \"conv\": 0,                  "
                  + "  \"cpm\": \"$0.22\",           "
                  + "  \"cpc\": \"$0.03\",           "
                  + "  \"ctr\": \"0.79%\",           "
                  + "  \"cpa\": \"$Inf\"             "
                  + "},                              "
                  + "\"382424\": {                   "
                  + "  \"imp\": 209,                 "
                  + "  \"clk\": 1,                   "
                  + "  \"spend\": \"$0.05\",         "
                  + "  \"conv\": 0,                  "
                  + "  \"cpm\": \"$0.23\",           "
                  + "  \"cpc\": \"$0.05\",           "
                  + "  \"ctr\": \"0.48%\",           "
                  + "  \"cpa\": \"$Inf\"             "
                  + "}}                              ";
    
          Total t = new Gson().fromJson("{" + json + "}", Total.class);
    
          System.out.println(t);
    
      }
    }
    

这会给你:

总计 [hour_totals={382423=HourData [imp=126, clk=1,spend=$0.03, conv=0, cpm=$0.22, cpc=$0.03, cpa=$Inf], 382424=HourData [imp=209, clk=1 , 花费=$0.05, 转化率=0, cpm=$0.23, cpc=$0.05, cpa=$Inf]}]

关于你的字符串。来自 JSON 官方语法(http://www.ietf.org/rfc/rfc4627.txt):

  1. JSON 语法

    JSON 文本是一系列标记。标记集包括六个结构字符、字符串、数字和三个文字名称。

    JSON 文本是一个序列化的对象或数组。

于 2013-10-05T21:42:43.773 回答