繁体   English   中英

尝试从 Json 字符串中获取值

[英]Try to get a Value from a Json String

我编写了一个将 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"
            }
         }
      }
   }
}

请任何人都可以帮助我吗?

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

你可以用杰克逊做到这一点。

<!--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 中读取每个子 JsonNode 来获取特定的子节点。

        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>

我找到了一个解决方案,它正在工作!

   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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM