Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.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_String - Fatal编程技术网

Java 将字符串与JSON字符串属性进行比较

Java 将字符串与JSON字符串属性进行比较,java,json,string,Java,Json,String,我有一个JSON字符串作为- [{"lv":[{"v":{"nt":"10;1341943000000","userId":622},"cn":0}, {"v":{"nt":"20;1234567890123","userId":622},"cn":0}, ] 在这个JSON字符串中,我将使用userId作为每个值的属性。可能在一个JSON字符串中,我有10个userId属性或15个userId属性。而userId将始终有一些数字 每个JSON字符串在userId中都有相同的数字。如果您看到上

我有一个JSON字符串作为-

[{"lv":[{"v":{"nt":"10;1341943000000","userId":622},"cn":0},
{"v":{"nt":"20;1234567890123","userId":622},"cn":0},
]
在这个JSON字符串中,我将使用
userId
作为每个值的属性。可能在一个JSON字符串中,我有10个userId属性或15个userId属性。而
userId
将始终有一些数字

每个JSON字符串在
userId
中都有相同的数字。如果您看到上面的JSON字符串,我将
622
作为每个
userId
的编号

现在,我尝试比较JSON字符串中的
id
userId
。我从其他方法获得
id
值,如下所示-

final int id = generateRandomId(random);
因此,
id
值应该与单个JSON字符串中的所有
userId
属性匹配

我将所有JSON字符串存储在
colData
List
中。目前,我正在尝试使用
包含
字符串
类的方法将
id
userId
匹配,我认为这是不正确的,因为只要找到一个匹配项,那么如果条件为真(这是错误的)

单个JSON字符串中可能存在20个用户id属性
,并且
19个用户id值
id
匹配,但一个
用户id
属性值不相同。因此,在我下面的代码中,该用例将失败。那么,我如何实现这个问题定义呢

for (String str : colData) {

   if (!str.contains(String.valueOf(id))) {

// log the exception here
handleException(ReadConstants.ID_MISMATCH, Read.flagTerminate);

   }
}

感谢您的帮助。

一种方法是使用Matcher

public class Uid {
    private static final Pattern USER_ID_PATTERN = Pattern.compile("userId\":\\d+");
    private static final String GENERATED_USER_ID = "userId\":622";
    public static void main(String[] args) {

        List<String> jsonData = new ArrayList<String>();
        jsonData.add("[{\"lv\":[{\"v\":{\"nt\":\"10;1341943000000\",\"userId\":621},\"cn\":0},{\"v\":{\"nt\":\"20;1234567890123\",\"userId\":622},\"cn\":0},]"); // this string contains multiple uids

        for (String s : jsonData) {
            Matcher matcher = USER_ID_PATTERN.matcher(s);
            while (matcher.find()) {
                String currentUid = matcher.group();
                 if (!currentUid.equals(GENERATED_USER_ID))
                    System.out.println("LOG exception, " + currentUid + " doesn't exists");

            }
        }
    }
}
公共类Uid{
私有静态最终模式USER\u ID\u Pattern=Pattern.compile(“userId\”:\\d+);
生成的私有静态最终字符串\u USER\u ID=“userId\”:622;
公共静态void main(字符串[]args){
List jsonData=new ArrayList();
jsonData.add(“[{“lv\”:[{“v\”:{“nt\”:“10;1341943000000\”,“userId\”:621},““cn\”:0},{“v\”:{“nt\”:“20;1234567890123\”,“userId\”:622},““cn\”:0},]”;//此字符串包含多个UID
for(字符串s:jsonData){
Matcher Matcher=用户\u ID\u模式。Matcher(s);
while(matcher.find()){
字符串currentUid=matcher.group();
如果(!currentUid.equals(生成的用户ID))
System.out.println(“日志异常,+currentUid+”不存在”);
}
}
}
}

谢谢sol4me的建议。你确定它能工作吗?在我的情况下,它不会进入while循环。你是否根据输入调整了生成的用户ID()?final int ID=“userId”+generateRandomId(random);是的,我就是这样做的。但结果相同。