【问题标题】:Try to get a Value from a Json String尝试从 Json 字符串中获取值
【发布时间】:2020-06-20 11:01:06
【问题描述】:

我编写了一个将 XML 转换为 Json 的应用程序,我尝试从 Json 中获取值“内容”,但我无法获取。 我只得到完整的文件,我无法达到给我所需价格值的“内容”值。

这是代码部分

                //From XML to Json
                JSONObject xmlToJson = XML.toJSONObject(result);
                //Get the Json from result
                JSONObject jsonObject = xmlToJson.getJSONObject("result");
                Log.i("ergebniss",jsonObject.toString());
                //Get the value plz (Works)
                String json1 = jsonObject.getString("plz");
                //Give it out to TextView
                xmltojson.setText(json1.toString());

这是我得到的 JSON 字符串

{
   "result":{
      "plz":87484,
      "taxRate":19,
      "deliveries":{
         "delivery":{
            "unloadingPoints":1,
            "litre":1000,
            "price":{
               "oilGrade":"Heizöl Standard Schwefelarm",
               "orderLink":"https://www.heizoel24.de/.../1/1000/1/24,9,11,5,6/67,78",
               "content":"67,78"
            }
         }
      }
   }
}

谁能帮我解决这个问题?

【问题讨论】:

  • 你试过使用xmlToJson.getJSONObject("price"); 吗?在第二行代码。
  • 是的,它什么也不返回。
  • Underscore-java库有一个静态方法U.get(map, path)。

标签: java android json xml


【解决方案1】:

您可以在您的应用程序中使用 Java Jackson jar API 将 xml 转换为 json 格式。它首先将数据直接绑定到 java 对象。它创建一个 XMLMapper 实例,并在其 readValue 方法的帮助下读取 xml 文件的内容。 它在 ObjectMapper 类的帮助下以 JSON 格式写下内容。

【讨论】:

    【解决方案2】:

    杰克逊可以做到这一点。

    <!--Converted xml from json-->
    <root>
       <result>
          <deliveries>
             <delivery>
                <litre>1000</litre>
                <price>
                   <content>67,78</content>
                   <oilGrade>Heizöl Standard Schwefelarm</oilGrade>
                   <orderLink>https://www.heizoel24.de/.../1/1000/1/24,9,11,5,6/67,78</orderLink>
                </price>
                <unloadingPoints>1</unloadingPoints>
             </delivery>
          </deliveries>
          <plz>87484</plz>
          <taxRate>19</taxRate>
       </result>
    </root>
    

    首先,将 XML 转换为 JSON,然后通过读取转换后的 JSON 中的每个 sub-JsonNodes 来获取特定的子节点。

            XmlMapper xmlMapper = new XmlMapper();
            JsonNode jsonNode = xmlMapper.readTree(xml.getBytes());
            JsonNode price = new ObjectMapper()
                    .valueToTree(jsonNode)
                    .get("result")
                    .get("deliveries")
                    .get("delivery")
                    .get("price");
    
            String content = price.get("content").asText(); // 67,78
    

    杰克逊依赖

            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-core</artifactId>
                <version>2.8.2</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
                <version>2.8.2</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-annotations</artifactId>
                <version>2.8.2</version>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.dataformat</groupId>
                <artifactId>jackson-dataformat-xml</artifactId>
                <version>2.8.2</version>
            </dependency>
    

    【讨论】:

    • 当我调试它时,它会在“delivery”处停止并返回一个异常 e
    • 它抛出了什么异常?我已经在机器上执行并且工作得很好
    • java.lang.NullPointerException: 尝试调用虚拟方法 'com.fasterxml.jackson.databind.JsonNode com.fasterxml.jackson.databind.JsonNode.get(java.lang.String)'空对象引用
    • 似乎 Json 数据可能不包含给定的键。你能检查一下来自 URL 的 json 数据吗?
    • {"plz":"87484","taxRate":"19","deliveries":{"delivery":{"unloadingPoints":"1","litre":"1000" ,"price":{"oilGrade":"Heizöl Standard Schwefelarm","orderLink":"heizoel24.de/bestellung/8","":"67,78"}}}}
    【解决方案3】:

    我找到了一个解决方案,它的工作原理!

       try {
                    jsonObj = XML.toJSONObject(s);                               // XML to Json
                    JSONObject result1 = jsonObj.getJSONObject("result");        // gets top level object
                    JSONObject deliveries = result1.getJSONObject("deliveries"); // get deliveries "collection" (Not a collection in this case)
                    JSONObject delivery = deliveries.getJSONObject("delivery"); // get price object (both properties and content)
                    JSONObject priceObj = delivery.getJSONObject("price");      // get price object (both properties and content)
                    String oilGrade = priceObj.getString("oilGrade");           // get the oilGrade
                    String price = priceObj.getString("content");               // get the price
    
                    afterValue = price.replace(',', '.');                       //replace the , with an . to work with a double .
                    double priceDouble = Double.parseDouble(afterValue);        //parse the String to a Double
    

    【讨论】:

      猜你喜欢
      • 2016-09-30
      • 2021-07-13
      • 1970-01-01
      • 2023-03-18
      • 1970-01-01
      • 2018-04-24
      • 2017-03-31
      • 2017-07-25
      • 1970-01-01
      相关资源
      最近更新 更多