【问题标题】:Extracting name/value pairs from JSON with Java使用 Java 从 JSON 中提取名称/值对
【发布时间】:2014-02-25 03:47:29
【问题描述】:

我有这个 JSON 代码:

{
"success": 1,
"item": [
    {
        "itemId": "jogurt123",
        "name": "Jogurt",
        "description": "kajmak",
        "pictureUrl": "https://www.google.hr/images/srpr/logo11w.png",
        "categoryId": "mlijeko"
    }
],
"specs": [
    {
        "specId": "volumen",
        "value": "1",
        "unit": "litra"
    },
    {
        "specId": "mast",
        "value": "50",
        "unit": "%"
    }
]

}

我想知道如何使用 Java 将名称/值对提取到字符串中。 我想得到最终结果:

String name = "Jogurt"
String description = "kajmak"
etc...

我尝试使用 JSONObject 创建一个包含此名称/值对的对象,然后我想提取它们,但在以下代码中

    String getParam(String code, String element){
    try {
        String base = this.getItembyID(code);
        JSONObject product = new JSONObject(base);
        String param = product.getString("name");
        return param;
    } catch (JSONException e) {
        e.printStackTrace();
        return "error";
    }
    }

我得到一个例外,说 JSONObject 中没有元素“名称”,但显然存在。有什么建议吗?

编辑:getItembyID 方法以字符串形式返回上面编写的 JSON 代码。 JSON 代码已经过验证

【问题讨论】:

  • 您是否转储(toString)product 对象以查看它的样子?您需要先了解 JSON 结构的结构,然后才能从中提取数据。

标签: java json parsing


【解决方案1】:

{ ->JSON 对象

[-> JSONArray

你需要获取Jsonarray里面的jsonObject

这样做

String getParam(String code, String element){
    try {
           String base = this.getItembyID(code);
           JSONObject product = new JSONObject(base);
          JSONArray jarray = product.getJSONArray("item");
         String param =  jarray.getJSONObject(0).getString("name");
  return param;
    } catch (JSONException e) {
        e.printStackTrace();
        return "error";
    }
    }

【讨论】:

    【解决方案2】:

    假设 base 是您在问题中发布的 JSON,那么您在此处说明的假设

    我得到一个异常,说在 显然有 JSONObject。

    错了。您显示的 JSON 对象仅包含 3 个元素:successitemspecsitem 是一个带有单个元素的 JSON 数组,即另一个 JSON 对象。该 JSON 对象包含一个名为 name 的值。

    您需要获取 that JSON 对象,以便您可以检索 that 值。

    或者考虑使用不同的 JSON 解析库,例如 Jackson 或 Gson,它们主要基于一些 POJO 类为您执行此操作。

    【讨论】:

    • 我将代码修改为 String getParam(String code, String element){ try { String base = this.getItembyID(code); JSONObject 产品 = 新 JSONObject(base); JSONArray item = product.getJSONArray("item");字符串参数 = (String) item.getString(1);返回参数; } catch (JSONException e) { e.printStackTrace();返回“错误”;但现在我得到另一个错误 JSONArray[1] not found。这是为什么呢?
    • @MislavJavor 不要在 cmets 中发布代码。很难阅读。 item 是一个 JSON 数组,它包含一个 JSON 对象,而不是字符串,并且不在索引 1 处。第一个元素在索引 0 处。
    • 对不起评论中的代码,不知道该怎么做。非常感谢您的回答
    【解决方案3】:

    如果你有特定数据的 java 类,你会得到 json 的形式。 然后使用jackson库很容易提取数据。 [下载:http://repo1.maven.org/maven2/com/fasterxml/jackson/core/jackson-core/2.2.3/jackson-core-2.2.3.jar]

    那么您的代码将是:

    public String getParam(String code, String element) {
    
        String base = this.getElementById(code);
        ObjectMapper mapper = new ObjectMapper();
        Product product = mapper.readValue(base, Product.class);
    
        // this will return name of product
        return product.getItem()[0].getName();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-04
      • 1970-01-01
      • 1970-01-01
      • 2014-04-04
      • 1970-01-01
      • 2020-11-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多