【问题标题】:How to parse custom objects from a list within a jsonstring?如何从 jsonstring 中的列表中解析自定义对象?
【发布时间】:2016-02-10 05:47:37
【问题描述】:

我得到了以下 json:

{
    "ID": "1234567",
    "dangereousCargo": true,
    "numberOfPassangers": 164,
    "cargo": [
        {
            "type": "Oil",
            "amount": 8556
        },
        {
            "type": "Chemicals",
            "amount": 5593
        }
    ]
}

this 的问题中,我了解到可以从 jsonObject 中获取 cargoList(如果该列表包含某种类型的对象)。但是如何从该列表中获取单独的 cargoObjects 呢?

+jsonstring的变量名一定要和我的CargoClass里面的变量名对应吗?如果 jsonObject 只包含类型和数量,而我的 CargoClass 有更多属性怎么办?

【问题讨论】:

标签: java json list parsing


【解决方案1】:

您可以迭代抛出JSONArray,它代表您的货物清单正在执行(未测试)

JSONObject json = new JSONObject(jsonString);
JSONArray cargoList = json.getJSONArray("cargo");

for(int i=0; i< cargoList.length(); i++) {
    JSONObject cargo = cargoList.getJSONObject(i);
    //Do something with cargo
}

【讨论】:

    【解决方案2】:

    使用 JSON Simple 可以很容易地从 JSON 中解析和读取数据。正如其他用户所说,有很多图书馆可以做到这一点。下面是一个经过测试的代码示例。

        public static void main(String[] args) throws JSONException {
        String json = "{\n"
                + "    ID: 1234567,\n"
                + "    dangereousCargo: true,\n"
                + "    numberOfPassangers: 164,\n"
                + "    cargo: [\n"
                + "        {\n"
                + "            type: Oil,\n"
                + "            amount: 8556\n"
                + "        },\n"
                + "        {\n"
                + "            type: Chemicals,\n"
                + "            amount: 5593\n"
                + "        }\n"
                + "    ]\n"
                + "}";
    
        JSONObject data = new JSONObject(json);
    
        int id = data.getInt("ID");
        boolean danger = data.getBoolean("dangereousCargo");
        int numOfPassengers = data.getInt("numberOfPassangers");
    
        System.out.println("Current ID: " + id + "\n"
                + "Is Dangerous: " + danger + "\n"
                + "Number of Passengers: " + numOfPassengers + "\n");
        JSONArray cargo = data.getJSONArray("cargo");
    
        NumberFormat currency = NumberFormat.getCurrencyInstance();
        for (int i = 0; i < cargo.length(); i++) {
            JSONObject cargoObject = cargo.getJSONObject(i);
            String type = cargoObject.getString("type");
            double amount = cargoObject.getDouble("amount");
            System.out.println("Current Type: " + type);
            System.out.println("Current Amount: " + currency.format(amount));
        }
    
      }
    
    }
    

    上面的代码给了我们以下输出:

    一旦你有了你的数据,你就可以用它做任何你想做的事情。

    JSONSimple-https://code.google.com/p/json-simple/

    GSON-https://github.com/google/gson

    【讨论】:

      【解决方案3】:

      但是如何从该列表中取出单独的 cargoObjects?

      String jsonString = "{ ... }";
      JSONObject json = new JSONObject(jsonString);
      JSONArray cargoList = json.getJSONArray("cargo");
      for(JSONObject cargo : cargoList)
      {
         //do something with your cargo element
      }
      

      jsonstring的变量名是否必须与 我的 CargoClass 中的变量名?

      如果您使用 JSONObject 中的 get 方法,您必须在 jsonString 中指定属性的确切名称。按照上面的例子:

      String cargoType = cargo.getString("type");
      

      顺便说一句,如果你想使用你已经定义的 CargoClass,你需要一个 Deserializer,并且你的 JSON 上的所有属性都必须在你的 CargoClass 上都存在并且都是一样的:我建议你看看 other SO的问题like this one

      如果 jsonObject 只包含 type 和 amount 以及我的 CargoClass 怎么办 有更多的属性吗?

      其他属性将在您的类声明的基础中初始化

      【讨论】:

      • 我本来打算接受 Maloubobola 的回答,但你的回答更完整,包含了我问题的所有方面。我将代码调整为我的项目,你可以在这里找到 pastebin.com/618nAEAV 。最后的话,我看到每个人都在使用 JSONObject 等。它对我不起作用,你在使用 javax.json.JsonObject 吗?
      • 不,我假设您使用了 org.json package。我认为这个更易读,但您也可以使用 Google 的 GSon lbrary
      猜你喜欢
      • 1970-01-01
      • 2018-05-16
      • 2019-10-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-04
      • 2015-12-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多