繁体   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();

这是我要尝试解析并获取值的代码,但是当我制作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"}

但无法解析

你必须写这样的代码

           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");

直接使用JSONObject解析POJO既繁琐又容易出错,建议使用以下库之一:

首先,定义您的POJO类,您可以使用一些在线服务,例如thisthis

只需粘贴您的示例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;

}

然后,要获取java对象,请使用以下gson调用:

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

如果使用Jackson或其他库,则应用相同的过程,并且它们的区别仅在于详细的函数调用。

暂无
暂无

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

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