本文介绍了处理JSON响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have Asp.net Api to get JSON response like this:

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

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

How can I handle this JSON response in Android Studio Using Volley Lib?

这是我的代码:

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 Object ..您可以按照以下步骤进行操作.

Most probably you are getting a string respone. If its a string then firstly conver it to Json Object.. you can do this as following below.

JSONObject jsonObject=new JSONObject(response);

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

If it is JSONObject already then try to conver it to java object as following:

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

将代码放入onResponse中.最后似乎您正在获取对象数组.然后创建一个POJO类,如下所示:

Put the code in your onResponse. and lastly seems like you are getting an array of object. Then create a POJO class like below:

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

然后在您的代码中声明:

Then in declare in your code:

ArrayList<UserData>userDataList=new ArrayList();

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

Follow the below method to set data to arraylist with json data parsing

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

这篇关于处理JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 23:32