如何解析JSON对象并获取数据在安卓

问题描述:

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

这是我的代码我试图JOSon解析并获得价值,但发生什么{,}在结果中删除时,我做的JSON对象,并试图和获得价值然后我得到Excepion我得到 将String.valueOf(((SoapObject)envelope.bodyIn).getProperty(0):如何解析JSON对象并获取数据在安卓

{"UserId":"2","UserParentId":"1","UserName":"Anilkumar","UserPassword":"12546", 
"UserMobile":"8130513899","UserEmail":"[email protected]","UserMpin":"7890", 
"UserBalance":"20.0000","UserResponseMessage":"Is Valid"} 

但无法

+0

逗号是不是你的问题,请分享你的错误代码 – Shailesh

+0

后异常堆栈跟踪。 –

+0

{“UserId”:“2”,“UserParentId”:“1”,“UserName”:“Anilkumar”,“UserPassword”:“12546”,“UserMobile”:“8130513899”,“UserEmail” “”UserMpin“:”7890“,”UserBalance“:”20.0000“,”UserResponseMessage“:”有效“}这里我到这里JSONObject _jobjec = new JSONObject(”{“+ result);但无法解析 –

直接使用的JSONObject解析POJO解析它是乏味的错误倾向于推荐使用以下库之一:

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

只需粘贴你的榜样JSON字符串,那么你可以得到以下POJO类(你不需要把它写在你自己的)2秒:

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

如果使用杰克逊或者其他库,同样的过程也适用,它们的区别仅在于具体的函数调用。

你必须这样写代码

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

我写了类似这样的,但仍然我得到异常 –

+0

org.json.JSONException:名称必须是字符串,这个异常正在得到 –

+0

我希望你采取String UserId,UserParentId,UserName,UserPassword,UserPassword ,UserPassword,UserEmail,UserMobile等。 –