0

I have two Strings, one is a JSON string( that is i created a json obj and converted it to string using obj.tostring() and passed it to another class) and an ordinary string with characters no words or meaning like "abcdefg".

This below is the json format string s1:

{"email_prompted":"n","error":"o","pin":"c","antiphishing_prompted":"n","antiphishing":"n"}

The other string is s2: "EoPnAn"

In String s2 the characters E represents error P represents pin and A represents antiphishing.

Now i want to compare the value of error in s1 "o" is equal to the second character in s2. that is i want to check the values of error, pin and antiphishing with second, fourth and sixth characters of s2.

And if all three are equal i want to print "ok".

I am not asking for u to write me a code but give me a hint on how to do it. But if u provide a code or example it is much appreciated.

The following is an over simplification of what i wanted. Sorry for my overly simple code but will this work?

String s1="{"email_prompted":"n","error":"o","pin":"c","antiphishing_prompted":"n","antiphishing":"n"}" ;

String s2="EoPnAn";

char[] p1 = s1.toCharArray();

char[] p2 = s2.toCharArray();

if(p1[31]==p2[1] && p1[41]==p2[3]&&p1[88]==p2[5]){

System.out.println("OK");

}

2
  • Can you give us the bunch of code you've done so far? Commented Mar 28, 2013 at 15:53
  • sorry i dont have code yet i am trying to use tokens with pattern match and split function. ill post the code in a few minutes thanks Commented Mar 28, 2013 at 16:03

2 Answers 2

1

In the example given the result is not "ok" (pin is "c", not "n"):

    String s1 = "{\"email_prompted\":\"n\",\"error\":\"o\",\"pin\":\"c\",\"antiphishing_prompted\":\"n\",\"antiphishing\":\"n\"}";

    String s2 = "EoPnAn";

    String[] s1Parts = s1.replaceAll("[\\{}\"]", "").split("[,:]");
    boolean equal = s2.substring(1, 2).equals(s1Parts[3]) /* error */
            && s2.substring(3, 4).equals(s1Parts[5]) /* pin */
            && s2.substring(5, 6).equals(s1Parts[9]); /* antiphishing */
    if(equal) {
        System.out.println("ok");
    }
Sign up to request clarification or add additional context in comments.

Comments

0

You could create a map of key values of the json string i.e. s1 using available java libraries - like Jackson or GSON

parse s2 and compare with the value from the above map

Comments

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.