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.

user2326860
  • 1
  • 1
  • 4
  • 12
  • 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) – lenik Apr 27 '13 at 13:01
  • Yes it is malformed... but from the above JSON object cant i get string array of "Name" values... – user2326860 Apr 27 '13 at 13:03
  • This response string you are getting in JSON format, Search how to parse JSON String or look into @gurvinder372 answer. – Vishesh Chandra Apr 27 '13 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 =) – lenik Apr 28 '13 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. – anton1980 Jun 27 '13 at 16:02

3 Answers3

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

Community
  • 1
  • 1
gurvinder372
  • 66,980
  • 10
  • 72
  • 94
  • 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 Apr 27 '13 at 12:57
  • @user2326860 Is this the console output or this is how you store it in your java variable? – gurvinder372 Apr 27 '13 at 12:59
  • @user2326860 It is unnecessary, replace all "\" with "" first so that there are no backslashes in your response – gurvinder372 Apr 27 '13 at 13:11
  • if (response.contains("\'")) { response = response.replaceAll("\'", ""); } I have done like this... but \ is not replacing.. can u pls correct it to replace \... – user2326860 Apr 27 '13 at 13:13
  • try this String str2 = "Str\\sdfsdf"; String str = "\\\\"; System.out.println( str2.replaceAll( str, "" ) ); – gurvinder372 Apr 27 '13 at 13:17
  • Thanks a lot for ur answer.... \ all r replaced... can u pls tell me now how to get Name value into String Array.... – user2326860 Apr 27 '13 at 13:32
  • @user2326860 first parse this json into the java object, then iterate the object depending upon which json parser you have used. – gurvinder372 Apr 27 '13 at 13:35
  • @user2326860 Please read my answer again, I have provided enough links to parse json – gurvinder372 Apr 27 '13 at 13:42
  • i am getting the exception from 1st link : JsonParser is not available.. Pls see the answer below....of Dev Blakened.. this code works.... but getting all the key annd values.. but i want only Name values... Can u pls help me.... – user2326860 Apr 27 '13 at 13:47
  • @user2326860 did you tried other links? what exception are you getting? Please open another question for that exception? – gurvinder372 Apr 27 '13 at 13:49
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);
    }
}
}
Gaurav
  • 71
  • 1
  • 14
  • 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... – user2326860 Apr 27 '13 at 13:10
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);
Dev Blanked
  • 8,555
  • 3
  • 26
  • 32
  • 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 – user2326860 Apr 27 '13 at 13:45
  • just do a if check against keyValue[0] and populate a list using keyValue[1] – Dev Blanked Apr 27 '13 at 13:47
  • @user2326860 how about now? – Dev Blanked Apr 27 '13 at 14:12
  • Thanks... it worked.. u made my day... Thanks a lot buddy...i dont have enough reputation to upvote... so acceptedddd.... – user2326860 Apr 27 '13 at 14:16