0

I want to extract the string returned from java web service in java client. The string returned from java web service is as follows:

{"Name":"Raj Johri","Email":"[email protected]","status":true}

Which is a Json string format. I have written client code to extract this string as follows:

public static void main(String[] args) throws Exception{
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpPost post = new HttpPost("http://localhost:8080/JsonWebService/services/JsonWebService/getData");
    post.setHeader("Content-Type", "application/xml");
    HttpResponse httpres = httpClient.execute(post);
    HttpEntity entity = httpres.getEntity();
    String json = EntityUtils.toString(entity).toString();
    System.out.println("json:" + json);
}

I am getting following print on the console for json as:

json:<ns:getDataResponse xmlns:ns="http://ws.jsonweb.com"><ns:return>{"Name":"Raj Johri","Email":"[email protected]","status":true}</ns:return></ns:getDataResponse>

Please tell me how to extract the string

{"Name":"Raj Johri","Email":"[email protected]","status":true}

which is the actual message. Thanks in advance...

0

2 Answers 2

1

Well, The respons is as type of xml, and your json is in the <ns:return> node , so i suggest you to enter in depth of the xml result and simply get your json from the <ns:return> node.

Note: I suggest you to try to specifying that you need the response as JSON type:

post.setHeader("Content-type", "application/json");
post.setHeader("Accept", "application/json");
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for response. I tried that but gives an exception xml:lang="en-US">com.ctc.wstx.exc.WstxEOFException
0

There is a dirty way to do this (beside the xml parsing way) if you are getting the same XML every time, you can use split()

String parts[] = json.split("<ns:return>");
parts = parts[1].split("</ns:return>");
String jsonPart = parts[0];

now jsonPart should contain only {"Name":"Raj Johri","Email":"[email protected]","status":true}

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.