Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.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 以包含一系列数字的字符串形式传递JSON键_Java_Json - Fatal编程技术网

Java 以包含一系列数字的字符串形式传递JSON键

Java 以包含一系列数字的字符串形式传递JSON键,java,json,Java,Json,我正在尝试使用一组键来指向一个值。 例如,键0指向值“example_1” 键1,2,3,4,5指向值“EXAMPLE_2” 键6,7,8,9,10指向值“EXAMPLE_3” 这是我提出的JSON结构(将存在于外部文件中) 使用以下代码读取并获取正确的值 private String getValue(String count){ Map<String, String> map = // code to fetch data from the file and get ab

我正在尝试使用一组键来指向一个值。 例如,键0指向值“example_1” 键1,2,3,4,5指向值“EXAMPLE_2” 键6,7,8,9,10指向值“EXAMPLE_3”

这是我提出的JSON结构(将存在于外部文件中)

使用以下代码读取并获取正确的值

private String getValue(String count){
    Map<String, String> map = // code to fetch data from the file and get above map. Works.

    for (Map.Entry<String, String> entry : map.entrySet()) {
        String key = entry.getKey();
        String value = entry.getValue();

        String[] keysInRange = key.split(",");

        if(Arrays.asList(keysInRange).contains(count)){
            return value;
        }
    }
}
private String getValue(字符串计数){
Map Map=//从文件中获取数据并获取上面的Map.Works的代码。
对于(Map.Entry:Map.entrySet()){
String key=entry.getKey();
字符串值=entry.getValue();
字符串[]keysInRange=key.split(“,”);
if(Arrays.asList(keysInRange.contains)(计数)){
返回值;
}
}
}
这在技术上是可行的,但有没有更好的方法来做到这一点

希望改进JSON结构。 发现以这种方式传递钥匙是愚蠢的。
请注意,这些键可以是单个数字,也可以始终在一个范围内

你可以在下面试试。这是假设,范围内的关键点类似于1,5的1,2,3,4,5

private String getValue(String count){
    Map<String, String> map = // code to fetch data from the file and get above map. Works.

    If(map.containsKey(count)){
     return map.get(count);
    }

    for (Map.Entry<String, String> entry : map.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();

            if(key.contains(","+count) || key.contains(","+count+",") || key.contains(count+",") ){
              return value;
            }      
        }
   }
private String getValue(字符串计数){
Map Map=//从文件中获取数据并获取上面的Map.Works的代码。
If(地图容器(计数)){
返回map.get(计数);
}
对于(Map.Entry:Map.entrySet()){
String key=entry.getKey();
字符串值=entry.getValue();
if(key.contains(“,”+count)| key.contains(“,“+count+”,”)| key.contains(count+”,”){
返回值;
}      
}
}

您可以在下面尝试。这是假设,范围内的关键点类似于1,5的1,2,3,4,5

private String getValue(String count){
    Map<String, String> map = // code to fetch data from the file and get above map. Works.

    If(map.containsKey(count)){
     return map.get(count);
    }

    for (Map.Entry<String, String> entry : map.entrySet()) {
            String key = entry.getKey();
            String value = entry.getValue();

            if(key.contains(","+count) || key.contains(","+count+",") || key.contains(count+",") ){
              return value;
            }      
        }
   }
private String getValue(字符串计数){
Map Map=//从文件中获取数据并获取上面的Map.Works的代码。
If(地图容器(计数)){
返回map.get(计数);
}
对于(Map.Entry:Map.entrySet()){
String key=entry.getKey();
字符串值=entry.getValue();
if(key.contains(“,”+count)| key.contains(“,“+count+”,”)| key.contains(count+”,”){
返回值;
}      
}
}

您可以将JSON结构更改为元素数组:

{[ {
     "name": "EXAMPLE_1",
     "from": "0",
     "to": "0"
   },
   {
     "name": "EXAMPLE_2",
     "from": "1",
     "to": "5"
   },
   {
     "name": "EXAMPLE_3",
     "from": "6",
     "to": "10"
   }
]}
并使用类似Jackson oder GSON的JSON解析器在类似

class Example {
    private String name;
    private int from;
    private int to;
    // ommitted getters & setters for brevity
}
然后,您的方法变成(使用Java 8+和streams api):

private String getValue(int count){
Set examples=…//从文件中获取数据的代码
可选匹配=examples.stream()
.filter(示例->示例.getFrom()>=count)

.filter(example->example.getTo()您可以将JSON结构更改为元素数组:

{[ {
     "name": "EXAMPLE_1",
     "from": "0",
     "to": "0"
   },
   {
     "name": "EXAMPLE_2",
     "from": "1",
     "to": "5"
   },
   {
     "name": "EXAMPLE_3",
     "from": "6",
     "to": "10"
   }
]}
并使用类似Jackson oder GSON的JSON解析器在类似

class Example {
    private String name;
    private int from;
    private int to;
    // ommitted getters & setters for brevity
}
然后,您的方法变成(使用Java 8+和streams api):

private String getValue(int count){
Set examples=…//从文件中获取数据的代码
可选匹配=examples.stream()
.filter(示例->示例.getFrom()>=count)
.filter(示例->示例.getTo()