Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获取特定键及其值_Java_Json - Fatal编程技术网

Java 获取特定键及其值

Java 获取特定键及其值,java,json,Java,Json,我有一个JSON数据,我需要找到任何特定的键及其值。如果该键在JSON中可用,我需要找到它们的isActive状态 例如,我需要找到键“featureCD”及其值为“EXLVL”。如果该键可用,我需要找到该“EXLVL”的isActive状态及其值 提前谢谢你在这件事上帮助我 这是我的json数据: [ { "featureCD": "EXLVL", "featureName": "Experience Personalization", "moduleName":

我有一个JSON数据,我需要找到任何特定的键及其值。如果该键在JSON中可用,我需要找到它们的isActive状态

例如,我需要找到键“featureCD”及其值为“EXLVL”。如果该键可用,我需要找到该“EXLVL”的isActive状态及其值

提前谢谢你在这件事上帮助我

这是我的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
 }
]

您应该研究JSON路径实现,例如可以用来解决您的需求的JSON路径实现。

您可以使用
optString
,如果指定的键不存在,它将返回空字符串(“”)<另一方面,如果键不存在,code>getString会抛出一个JSONException

大概

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");
}
医生


此外,这将有助于您可以使用
ObjectMapper

首先创建您的类

public class JsonData
{
    private String featureName;

    private String featureCD;

    private String moduleName;

    private Boolean isActive;

    // getter setter
}
那就做吧

ObjectMapper mapper = new ObjectMapper();
List<JsonData> jsonData= mapper.readValue(jsonString, new TypeReference<List<JsonData>>(){});
ObjectMapper mapper=new ObjectMapper();
List jsonData=mapper.readValue(jsonString,new TypeReference(){});
然后,瞧,你可以用你的对象列表做任何你想做的事情。比如说

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

使用
JsonPath
它非常简单且版本丰富。特别是如果你使用POJO

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>
JSON由对象的fo数组组成。 我们需要创建一个与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;
}
然后我们将JSON解析成如下Java对象。注意,我们有一个对象数组,所以我们将其解析为数组

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

希望有帮助

也许这也可以解决你的答案:我的json是一个jsonArray,所以当我使用JSONObject json=new JSONObject()时,这将抛出一个错误,永远不会进入下一步执行…只需替换为
jsonArray jsonArray=new jsonArray(jsonStr)取决于您的要求。我只能提供一个概括性的想法。执行权留给你!
List<FeatureObject> features = Arrays.asList(path.getObject("$", FeatureObject[].class));
Boolean isFeatureActive = features.stream().filter(x -> x.featureCD.equals("EXLVL")).map(x -> x.isActive).findFirst().get();
System.out.println(isFeatureActive);