0

While working with android Socket.io nodejs server...

Server returns a list of users data

    private Emitter.Listener onSyncdData = new Emitter.Listener() {
        @Override
        public void call(final Object... args) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {                   
                    try {
                        Gson gson=new Gson();
                        String json = gson.toJson(args);

json string contains

[{"values":
[{"nameValuePairs":{"Name":"Varun","EmailAddress":"","Latitude":"12","Langitude":"12","IsYours":"false","MobileNumber":"(903) 011-7411"}},
{"nameValuePairs":{"Name":"Varun","EmailAddress":"","Latitude":"12","Langitude":"12","IsYours":"false","MobileNumber":"(905) 970-8111"}},
{"nameValuePairs":{"Name":"Viany","EmailAddress":"","Latitude":"12","Langitude":"12","IsYours":"false","MobileNumber":"(900) 094-3111"}},
{"nameValuePairs":{"Name":"Pavan","EmailAddress":"","Latitude":"12","Langitude":"12","IsYours":"false","MobileNumber":"(779) 981-3111"}},
{"nameValuePairs":{"Name":"Rakesh","EmailAddress":"","Latitude":"12","Langitude":"12","IsYours":"false","MobileNumber":"(800) 895-3111"}},
{"nameValuePairs":{"Name":"","EmailAddress":"","Latitude":"12","Langitude":"12","IsYours":"true","MobileNumber":"15555215554"}}
]}]

My Question is how can i retrieve "Name","EmailAddress","MobileNumber"..etc from json or args object.

1
  • If you want to read a JSON, the GSON method is called fromJson Commented Mar 18, 2016 at 7:12

1 Answer 1

1

Gson takes in JsonElement but not JSONObject(org.json). So you need to do as follows in your code so that you don't get the nameValuePairs:

JSONObject obj = (JSONObject)args[0];//be sure to org.json but not com.google.gson 
PrasingClass parsingClass = gson.fromJson(obj.toString(),PrasingClass.class);

to parse it correct without nameValuePairs.

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

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.