简体   繁体   English

在没有 Java Object class 的情况下提取值的最佳方法是什么

[英]What is the best way to extract a value without having a Java Object class

I have a string like below我有一个像下面这样的字符串

{
    "id": "abc",
    "title": "123.png",
    "description": "fruits",
    "information": [
        {
            "type": "apple",
            "url": "https://apple.com"
        },
        {
            "type": "orange",
            "url": "https://orange.com"
        }
    ],
    "versions": 0
}

I want to get the value of url where type: orange .我想获取url的值,其中type: orange The list in information may not always be in same order as appearing in the data above. information中的列表可能并不总是与上述数据中出现的顺序相同。 I know I could do it easily in python with json.loads and json.dump .我知道我可以在 python 中使用json.loadsjson.dump轻松完成。

I am trying to do it java using JsonNode and objectMapper.readTree.at("/information") but I am unable to get past this point in a clever neat way to get the list and fetch the url where type = orange.我正在尝试使用JsonNodeobjectMapper.readTree.at("/information")来完成 java 但我无法以一种巧妙巧妙的方式通过这一点来获取列表并获取 url,其中类型 = 橙色。

This is pretty straightforward这很简单

Use a JSON library and parse the response using the library.使用JSON库并使用该库解析响应。 Then get only the values and attributes that you need...然后只获取您需要的值和属性...

Example relevant to your case:与您的案例相关的示例:

// Get your Json and transform it into a JSONObject

JSONObject mainObject = new JSONObject(yourJsonString); // Here is your JSON...

// Get your "information" array

JSONArray infoArray = mainObject.getJSONArray("information"); // Here you have the array

// Now you can go through each item of the array till you find the one you need

for(int i = 0 ; i < infoArray.length(); i++)
{
    JSONObject item = participantsArray.getJSONObject(i);

    final String type = item.getString("type");
    final String url = item.getString("url");

    if(type.equals("orange"))
    {
        // DO WHATEVER YOU NEED
    }
}

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

相关问题 在Java中维护事务列表的最佳方法是什么(事务对象具有ID,日期,金额等字段) - what is the best way to maintain a list of Transactions in java (transaction object having fields like id, date, amount etc) 计算Java类的校验和的最佳方法是什么? - What is the best way to calculate a checksum for a Java class? 在Java中清理Object的最佳方法是什么? - What is the best way to clean up an Object in Java? 在 Java 中,确定 object 大小的最佳方法是什么? - In Java, what is the best way to determine the size of an object? Java:读取序列化对象的最佳方法是什么? - Java: What is the best way to read a Serialized object? 使用Java提取zip文件的最佳方法是什么 - What is the best way to extract a zip file using java 从 Java 中的字符串中提取第一个单词的最佳方法是什么? - What is the best way to extract the first word from a string in Java? 从Java中的字符串中提取此int的最佳方法是什么? - What is the best way to extract this int from a string in Java? 如何从Java中的BufferedReader对象中提取整个内容的最佳方法是什么? - How is the best way to extract the entire content from a BufferedReader object in Java? 什么是最有价值的Java对象数据库? - What's the best value Java object database?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM