简体   繁体   中英

how to Parse Json Object and get Data in android

   SoapObject data=(SoapObject) envelope.bodyIn;
                String result = String.valueOf(((SoapObject) envelope.bodyIn).getProperty(0));

                JSONObject _jobjec = new JSONObject(result);
                UserId = _jobjec.get("UserId").toString();
                UserParentId = _jobjec.get("UserParentId").toString();
                UserName = _jobjec.get("UserName").toString();
                UserPassword = _jobjec.get("UserPassword").toString();
                UserMobile = _jobjec.get("UserMobile").toString();
                UserEmail = _jobjec.get("UserEmail").toString();
                UserMpin = _jobjec.get("UserMpin").toString();

This is my COde i am trying to JOSon Parse and get value but what happen { ,} remove in result when i make json Object and trying and get value then i get Excepion i am getting String.valueOf(((SoapObject) envelope.bodyIn).getProperty(0) :

{"UserId":"2","UserParentId":"1","UserName":"Anilkumar","UserPassword":"12546",
"UserMobile":"8130513899","UserEmail":"anilaat87@gmail.com","UserMpin":"7890",
"UserBalance":"20.0000","UserResponseMessage":"Is Valid"}

but unable to parse it

you have to write code like this

           SoapObject data=(SoapObject) envelope.bodyIn;
            String result = String.valueOf(((SoapObject)envelope.bodyIn).getProperty(0));

            JSONObject _jobjec = new JSONObject(result);
            UserId = _jobjec.getString("UserId");
            UserParentId = _jobjec.getString("UserParentId");
            UserName = _jobjec.getString("UserName");
            UserPassword = _jobjec.getString("UserPassword");
            UserMobile = _jobjec.getString("UserMobile");
            UserEmail = _jobjec.getString("UserEmail");
            UserMpin = _jobjec.getString("UserMpin");

Directly using JSONObject to parse POJO is tedious and error prone, recommended using one of the below libraries:

First, define your POJO class, you can use some online service, eg this one or this ,

Just paste your example json string, then you can get below pojo class (you don't need to write it on your own) in 2 seconds:

package com.example;

import javax.annotation.Generated;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;


public class Example {

    @SerializedName("UserId")
    @Expose
    public String userId;
    @SerializedName("UserParentId")
    @Expose
    public String userParentId;
    @SerializedName("UserName")
    @Expose
    public String userName;
    @SerializedName("UserPassword")
    @Expose
    public String userPassword;
    @SerializedName("UserMobile")
    @Expose
    public String userMobile;
    @SerializedName("UserEmail")
    @Expose
    public String userEmail;
    @SerializedName("UserMpin")
    @Expose
    public String userMpin;
    @SerializedName("UserBalance")
    @Expose
    public String userBalance;
    @SerializedName("UserResponseMessage")
    @Expose
    public String userResponseMessage;

}

Then to get the java object, use below gson calls:

Gson gson = new GsonBuilder().setPrettyPrinting().create();
Example instance = gson.fromJson(jsonString, Example.class);

If you use Jackson or other libraries, the same process applies, and they differ only in the detailed function calls.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM