简体   繁体   English

获取特定密钥及其值

[英]Get Particular key and their value

I have a JSON Data and I need to find any particular key and their Value if that key is available in that json, I need to find their isActive status. 我有一个JSON数据,如果该键在json中可用,我需要找到任何特定的键及其值,我需要找到它们的isActive状态。

For example, I need to find the key "featureCD" and their value as "EXLVL" if this key is available, I need to find out the isActive status and their value of that "EXLVL" 例如,如果此密钥可用,我需要找到密钥“ featureCD”及其值作为“ EXLVL”,我需要找出isActive状态及其“ EXLVL”的值

Thanks in advance for helping me out on this. 在此先感谢您为我提供帮助。

This is my json data: 这是我的json数据:

    [
  {
   "featureCD": "EXLVL",
   "featureName": "Experience Personalization",
   "moduleName": "Builder",
   "isActive": true
 },
 {
   "featureCD": "FCLDR",
   "featureName": "Calendar Date UI Update",
   "moduleName": "Builder",
   "isActive": true
},
{
  "featureCD": "FDNID",
   "featureName": "Document Name in Download",
  "moduleName": "Builder",
  "isActive": true
},
{
  "featureCD": "FDNST",
  "featureName": "Do Not Show Tips Toggle",
  "moduleName": "Dashboard",
  "isActive": true
},
{
  "featureCD": "Feature_201",
  "featureName": "sDneB",
  "moduleName": "Dashboard",
  "isActive": true
},
{
  "featureCD": "Feature_410",
  "featureName": "mOnuz",
  "moduleName": "Dashboard",
  "isActive": true
},
{
  "featureCD": "Feature_515",
  "featureName": "TestTDhSE",
  "moduleName": "Dashboard",
  "isActive": true
},
{
  "featureCD": "FGYRU",
   "featureName": "Graduation Year Update",
  "moduleName": "Builder",
  "isActive": true
},
{
  "featureCD": "FMEDP",
 "featureName": "RWZ Mobile  Email Download Popup",
  "moduleName": "Builder",
  "isActive": true
 },
 {
  "featureCD": "FRMTI",
  "featureName": "RWZ Mobile TTC Interaction",
   "moduleName": "Builder",
  "isActive": true
},
 {
   "featureCD": "HSTAD",
   "featureName": "Hide Street Address",
   "moduleName": "Builder",
   "isActive": true
 },
 {
   "featureCD": "OPNTD",
   "featureName": "Open Template Drawer - EB-9307",
   "moduleName": "Builder",
   "isActive": true
 },
 {
  "featureCD": "SAUTO",
   "featureName": "Smart Autocomplete EB-9682",
   "moduleName": "Builder",
   "isActive": true
 }
]

You can use optString which returns the empty string ("") if the key you specify doesn't exist. 如果您指定的键不存在,则可以使用optString返回空字符串(“”)。 getString on the other hand throws a JSONException if the key does not exist. 另一方面,如果键不存在,则getString抛出JSONException。

Something like, 就像是,

JSONArray jsonarray = new JSONArray(jsonStr);
// Logic to Iterate through JSONArray, and get individual JSONObject
if(json.optString("featureCD").equals("EXLVL"){
     boolean isActive = json.getBoolean("isActive");
}

Docs here . 文档在这里

Also, this would help https://www.baeldung.com/java-org-json 另外,这将有助于https://www.baeldung.com/java-org-json

您应该研究JSON路径实现,例如https://github.com/json-path/JsonPath ,可以用来解决您的需求。

You can use ObjectMapper . 您可以使用ObjectMapper

First create your class 首先创建您的课程

public class JsonData
{
    private String featureName;

    private String featureCD;

    private String moduleName;

    private Boolean isActive;

    // getter setter
}

Then do 然后做

ObjectMapper mapper = new ObjectMapper();
List<JsonData> jsonData= mapper.readValue(jsonString, new TypeReference<List<JsonData>>(){});

And then voila you can do whatever you want with your list of objects. 然后瞧,您可以使用对象列表做任何您想做的事情。 For instance 例如

List<JsonData> ExlvlAndActiveData = jsonData.stream().filter(data->data.getFeatureCd().equals("EXLVL") && data.isActive).collect(Collectors.toList);

Using JsonPath it's very simple and versitale. 使用JsonPath ,非常简单且通用。 Especially if you use POJO. 特别是如果您使用POJO。

Maven dependency: Maven的依赖:

<!-- https://mvnrepository.com/artifact/io.rest-assured/json-path -->
<dependency>
    <groupId>io.rest-assured</groupId>
    <artifactId>json-path</artifactId>
    <version>4.1.1</version>
</dependency>

Usage: 用法:

JsonPath path = new JsonPath.from(json); //String or File 

Your JSON consists of fo Array of Objects. 您的JSON由对象数组组成。 We need to create an object which corresponds to your JSON Object in java like this: 我们需要在Java中创建一个与您的JSON对象相对应的对象,如下所示:

public class FeatureObject { //name irrelevant
    public String featureCD; //this name corresponds the name in JSON
    public String featureName; //it's crucial!
    public String moduleName;
    public Boolean isActive;
}

and then we parse JSON into Java Objects like this. 然后像这样将JSON解析为Java对象。 Note that we have Array of objects so we parse it to array 请注意,我们拥有对象数组,因此我们将其解析为数组

List<FeatureObject> features = Arrays.asList(path.getObject("$", FeatureObject[].class));

Now, all we have to do is get the desired feature from the List and check it's active status. 现在,我们要做的就是从List获得所需的功能并检查其活动状态。

I used Java Stream API but you can use regular for loop. 我使用了Java Stream API,但可以使用常规的for循环。

Example: 例:

Boolean isFeatureActive = features.stream().filter(x -> x.featureCD.equals("EXLVL")).map(x -> x.isActive).findFirst().get();
System.out.println(isFeatureActive);

Hope it helps! 希望能帮助到你!

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

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