0

I have a JSON String as-

[{"lv":[{"v":{"nt":"10;1341943000000","userId":622},"cn":0},
{"v":{"nt":"20;1234567890123","userId":622},"cn":0},
]

In this JSON String I will be having userId as the property for each values. It might be possible that in a Single JSON String I am having 10 userId property or 15 userId property. And userId will have some number always.

Each JSON String will have same number in userId. If you see the above JSON String, I have 622 as the number for each userId.

Now I am trying to compare id with userId in the JSON String. I am getting id value from some other means, like this-

final int id = generateRandomId(random);

So id value should be matching with all the userId property in a single JSON String.

And I am storing all the JSON String in colData List<String>. And currently I am trying to match id with userId using contains method of String class, which I believe is not correct because as soon as it finds one match then if conditions will get true (Which is wrong).

It might be possible that in a Single JSON String 20 userId properties are there and 19 userId values are matching with id but one userId property value is not same. So that use case will get failed in my below code. So how can I achieve this problem definition

for (String str : colData) {

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

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

   }
}

Thanks for the help.

1 Answer 1

2

One approach is to use 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");

            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks sol4me for the suggestion. Are you sure, its working, bcoz it is not going inside the while loop in my case.
Did you adjusted GENERATED_USER_ID() according to your input? final int id = "userId" + generateRandomId(random);
Yeah I did that exactly as it is. But same result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.