【问题标题】:Handling a JSON Response处理 JSON 响应
【发布时间】:2019-02-13 09:54:20
【问题描述】:

我有 Asp.net Api 来获得这样的 JSON 响应:

{\"UserID\":5,\"UserName\":\"asd\",\"Password\":\"asd\",\"Email\":\"ss@asd\",\"PhoneNumber\":\"1213\",\"Logtit\":0.0,\"Latitle\":0.0,\"OfGroup\":\"a \"}

如何使用 Volley Lib 在 Android Studio 中处理这个 JSON 响应?

这是我的代码:

public class MainActivity extends AppCompatActivity {


RequestQueue  requestqueue;
Button start;
TextView textView;
EditText ee;
public String givenValue = ee.getText().toString();
public String URL = "http://localhost:61511:8010/api/Feedback/5" ;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    requestqueue=Volley.newRequestQueue(this);
   final Button start =(Button)findViewById(R.id.btnget);
   final TextView textView =(TextView)findViewById(R.id.mTextView);
   final EditText ee =(EditText)findViewById(R.id.idtxt) ;
    start.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, URL, null, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        JSONArray jsonArray = response.getJSONArray("User");
                        for(int i= 0 ;i < jsonArray.length();i++){
                            JSONObject User = jsonArray.getJSONObject(i);

                        }
                    }catch (JSONException e ) {
                        e.printStackTrace();
                    }
                }

            },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Log.e("Volley", "ERROR");
                        }
                    }
            );
            requestqueue.add(jsonObjectRequest);}
    });
}

【问题讨论】:

  • 你得到的都是 json 吗?
  • 是的,这就是我得到的所有 Json
  • "{\"UserID\":5,\"UserName\":\"asd\",\"Password\":\"asd\",\"Email\":\" ss@asd\",\"PhoneNumber\":\"1213\",\"Logtit\":0.0,\"Latitle\":0.0,\"OfGroup\":\"a \"}"
  • @Ahmed 你到底想用这个响应 JSONObject 做什么
  • 如果你想转换成 Java 对象,你可以使用 Gson 来实现

标签: android json android-volley


【解决方案1】:

很可能你得到了一个字符串响应。如果它是一个字符串,则首先将其转换为 Json 对象。您可以按照以下方式执行此操作。

JSONObject jsonObject=new JSONObject(response);

如果它已经是 JSONObject 则尝试将其转换为 java 对象,如下所示:

int userId=jsonObject.getInt("UserID");
String userName=jsonObject.getString("UserName");
String pass=jsonObject.getString("Password");

将代码放入您的 onResponse 中。最后似乎你得到了一个对象数组。然后创建一个 POJO 类,如下所示:

    public class UserData {
    @Expose
    @SerializedName("UserId")
    private int userId;
    @Expose
    @SerializedName("UserName")
    private String userName;
    .
    .
    .
   //add the extra attribute and create getter and setter

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
} 

然后在你的代码中声明:

ArrayList<UserData>userDataList=new ArrayList();

按照下面的方法,通过json数据解析将数据设置为arraylist

int userId=jsonObject.getInt("UserID");
String userName=jsonObject.getString("UserName");
String pass=jsonObject.getString("Password");    
UserData userInfo=new UserData();
userInfo.setUserId(userId)
userIfo.setUserName(userName);
//add the other attribute similiarly
userDataList.add(userInfo);

【讨论】:

  • Tnx 为您提供帮助我处理我的代码你能在答案中看到吗
【解决方案2】:

您可以使用 GSON 将其解析为 Map。 更多信息请参考this问答。

【讨论】:

    猜你喜欢
    • 2016-11-05
    • 1970-01-01
    • 1970-01-01
    • 2012-05-15
    • 2020-05-14
    • 2019-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多