1

I am new to Java. Please help me. Below is the string after implementing HTTP POST method as a response:

 {"Result":"{  \"Search\": [    {      \"Code\": \"200\"    },    {      \"Message\": \"Fetched successfully\"    },    {      \"Name\": \"1\",      \"id\": \"166\",      \"PID\": \"162\"    }  ]}"}

Now i want to get only Names in the given String and form a Stirng[] of Names. Please help me.

5
  • your JSON is malformed, you should check your server code to avoid double JSON conversion on your data (notice the \" elements, there should be none of these, just the double-quotes, no slashes) Commented Apr 27, 2013 at 13:01
  • Yes it is malformed... but from the above JSON object cant i get string array of "Name" values... Commented Apr 27, 2013 at 13:03
  • This response string you are getting in JSON format, Search how to parse JSON String or look into @gurvinder372 answer. Commented Apr 27, 2013 at 13:11
  • @user2326860 if you cannot change the server part, your best bet would be to do a double JSON conversion, first to get the contents of GetEpExamsResult as a string, then once again parse this string as JSON to get your data in the proper format. however, you preferred to accept the hacky and kludgy answer, that took several tries to get it working and that will eventually stop working at any moment in the future. well, good luck with your choice =) Commented Apr 28, 2013 at 17:28
  • this is JSON and you really should be parsing it as such. if the problem is that it is malformed - then perhaps that is what needs fixing. Commented Jun 27, 2013 at 16:02

3 Answers 3

3

You need to first parse this JSON and then iterate through it

http://www.mkyong.com/java/json-simple-example-read-and-write-json/

parsing json with java

https://stackoverflow.com/questions/338586/a-better-java-json-library?rq=1

After you have parsed this JSON text, then its a matter of iterating through the object and keep pushing the values into arraylist

Once you got the arraylist, then you can convert it to a String array

Converting 'ArrayList<String> to 'String[]' in Java

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

10 Comments

Yes i already read these links.. But here the problem is Json array having "\". So i cannot get the JSon array correctly. Can u pls provide me how to form String Array of "Name" key in the above placed String???
@user2326860 Is this the console output or this is how you store it in your java variable?
@user2326860 It is unnecessary, replace all "\" with "" first so that there are no backslashes in your response
if (response.contains("\'")) { response = response.replaceAll("\'", ""); } I have done like this... but \ is not replacing.. can u pls correct it to replace \...
try this String str2 = "Str\\sdfsdf"; String str = "\\\\"; System.out.println( str2.replaceAll( str, "" ) );
|
1

You can use the below code to extract the names and their values

    List<String> nameValues = new ArrayList<String>();
    Matcher matcher1 = Pattern.compile("\\\\\"Name\\\\\":\\s\\\\\"[^,}\\]]+\\\\\"").matcher(value);
    while (matcher1.find()) {
        String keyValuePair = matcher1.group();
        String[] keyValue = keyValuePair.split(":");
        String val = keyValue[1].trim();
        System.out.println(keyValue[0]);
        System.out.println(">" + val + "<");
        nameValues.add(val.substring(2, val.length() - 2));
    }
    System.out.println(nameValues);

4 Comments

Thanks a lot... but by using ur code, i am getting every key and value for ex. i am getting code, message, name, categoryId... but i want only Name... can u pls modify the code such a way that i am able to retrieve only Name value and store in a String array.... Plssss
just do a if check against keyValue[0] and populate a list using keyValue[1]
@user2326860 how about now?
Thanks... it worked.. u made my day... Thanks a lot buddy...i dont have enough reputation to upvote... so acceptedddd....
1

Here use this one:

public class CStringToCharArray {
 public static void main(String[] args) {
    String testString = "This Is Test";
    char[] stringToCharArray = testString.toCharArray();

    for (char output : stringToCharArray) {
        System.out.println(output);
    }
}
}

1 Comment

So, using ur code i am getting each and every char as an output... but i want only string key'ed as "Name"... Can u provide code for that plss...

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.