开发者

Splitting up the Response from a server that has been derieved from JSON Object

开发者 https://www.devze.com 2023-04-06 07:13 出处:网络
I\'ve a task to spli开发者_StackOverflow社区t apart the response from a web server after User Name and password have been passed.

I've a task to spli开发者_StackOverflow社区t apart the response from a web server after User Name and password have been passed.

I've attached an image that will throw some more light in my problem

Splitting up the Response from a server that has been derieved from JSON Object

As you can see in the highlighted line I want to retrieve each parameter separately and display it on logcat. What type of functions should I be using? Also some link to provide me with an example would be much helpful.

Cheers


you can use GSON library for achieving the same EXAMPLE


from what you've written I am presuming your getting the response from the server as a JSONObject and you want to pass that.

If you are using JSONObject then

JSONObject jsonObj = new JSONObject("resultString"); 
JSONObject sessionJson =  jsonObj.getJSONObject("session");
String sessionId = sessionJson.getString("sessionid");
String name = sessionJson.getString("name");
Log.v("The name is"+name,"The session id is "+sessionId);

Here resultString is the the JSONObject that you will be recieving as a reponse from the server. The first part of the parse that you need to do is stored in "sessionJson"

{"session":{"sessionid":"0x000000000176f141:1000:125","name":"Andreas Öh"}}

Your second part i.e from "sessionid" will be stored in sessionId, and it goes on till you get the name.

Hope you find the explanation useful. Good luck !


This should help you get started:

JSONObject object=null;
try {
    object = (JSONObject) new JSONTokener(response).nextValue();

    JSONObject session=object.getJSONObject("session");
    String sessionid=session.getString("sessionid");
} catch (JSONException e) {
    e.printStackTrace();
}


You should use JSON.

In your case you can retrieve the name and sessionid like this :

     //Get the session json
     JSONObject respJson=new JSONObject(resonseString).getJSONObject("session");
     String name=respJson.getString("name");
     String sessionId=respJson.getString("sessionId");
0

精彩评论

暂无评论...
验证码 换一张
取 消