简体   繁体   English

如何解析Json对象并在Android中获取数据

[英]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) : 这是我要尝试解析并获取值的代码,但是当我制作json对象并尝试获取值时,发生的结果{,}会删除结果,然后我得到Excepion,得到String.valueOf((((SoapObject)capsule.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: 直接使用JSONObject解析POJO既繁琐又容易出错,建议使用以下库之一:

First, define your POJO class, you can use some online service, eg this one or this , 首先,定义您的POJO类,您可以使用一些在线服务,例如thisthis

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: 只需粘贴您的示例json字符串,即可在2秒钟内进入pojo类(您无需自己编写):

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: 然后,要获取java对象,请使用以下gson调用:

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. 如果使用Jackson或其他库,则应用相同的过程,并且它们的区别仅在于详细的函数调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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