首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >解析JsonObjectRequest

解析JsonObjectRequest
EN

Stack Overflow用户
提问于 2017-10-10 10:30:13
回答 3查看 823关注 0票数 2

我是Android和JAVA的新手,我正试图解析一个json响应。我知道如何解析jsonarray,但不知道如何解析jsonobject。有人能告诉我怎么做吗?以下是我的回应。

代码语言:javascript
复制
{"118":{"garment_color":"Blue","garment_name":"skjhkds","garment_price":"232"},"119":{"garment_color":"hjsadjjs","garment_name":"sdasd","garment_price":"23478"}}

这就是如何解析jsonarray。

代码语言:javascript
复制
public void JSON_DATA_WEB_CALL(){

    jsonArrayRequest = new JsonArrayRequest(GET_JSON_DATA_HTTP_URL,

            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                progressBar.setVisibility(View.INVISIBLE);
                    JSON_PARSE_DATA_AFTER_WEBCALL(response);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            });

    requestQueue = Volley.newRequestQueue(this);

    requestQueue.add(jsonArrayRequest);
}

public void JSON_PARSE_DATA_AFTER_WEBCALL(JSONArray array){

    for(int i = 0; i<array.length(); i++) {

        GetDataAdapter GetDataAdapter2 = new GetDataAdapter();

        JSONObject json = null;
        try {

            json = array.getJSONObject(i);

            GetDataAdapter2.setImageTitleNamee(json.getString(JSON_IMAGE_TITLE_NAME));
            //GetDataAdapter2.setImageServerLarger(json.getString(JSON_IMAGE_LARGER));

            GetDataAdapter2.setImageServerUrl(json.getString(JSON_IMAGE_URL));
            GetDataAdapter2.setMrp_price(json.getString(JSON_MRP_PRICE));
            GetDataAdapter2.setDisc_price(json.getString(JSON_DISC_PRICE));

        } catch (JSONException e) {

            e.printStackTrace();
        }
        GetDataAdapter1.add(GetDataAdapter2);
    }

    recyclerViewadapter = new RecyclerViewAdapter(GetDataAdapter1, this);

    recyclerView.setAdapter(recyclerViewadapter);
}

拜托有人帮忙。谢谢。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2017-10-10 10:33:40

在我看来,使用葛森库,给它json对象/数组/字符串,它会自动地将它解析成一个java对象。注意,您必须用适当的字段定义java类。

编辑:下面是一个与建议的指导方针相一致的答案:

首先,创建模型类,就像从服务器接收它们一样:

代码语言:javascript
复制
public class MyServerObject {
    MyGarment jsonKeyName;
}

public class MyGarment {
    String garment_color;
    String garment_name;
    String garment_price;
}

接下来,在收到json字符串后,使用Gson解析它:

代码语言:javascript
复制
Gson gson = new Gson();
String json= "{"jsonKeyName":{"garment_color":"Blue","garment_name":"skjhkds","garment_price":"232"};

MyServerObject serverObject = gson.fromJson(json, MyServerObject.class);

现在,您可以使用正确解析的所有值从服务器对象访问Garment对象。还请注意,如果收到json数组,可以将对象作为列表添加到MyServerObject.class中。

希望这能有所帮助。

票数 1
EN

Stack Overflow用户

发布于 2017-10-10 10:46:22

根据我上面的评论

您需要发出JSONObject请求而不是JSONArray请求

尝试这个来解析您的JSON响应

代码语言:javascript
复制
try {
            JSONObject jsonObject= new JSONObject("Response");

            JSONObject jsonObject1=jsonObject.getJSONObject("118");
            String garment_color=jsonObject1.getString("garment_color");
            String garment_name=jsonObject1.getString("garment_name");
            String garment_price=jsonObject1.getString("garment_price");

            JSONObject jsonObject2=jsonObject.getJSONObject("119");
            String garment_color2=jsonObject1.getString("garment_color");
            String garment_name2=jsonObject1.getString("garment_name");
            String garment_price2=jsonObject1.getString("garment_price");


        } catch (JSONException e) {
            e.printStackTrace();
        }
票数 0
EN

Stack Overflow用户

发布于 2017-10-10 12:10:23

使用StringRequest代替JSONObject/JSONArray请求,最后获取如下值:

代码语言:javascript
复制
JSONObject object = new JSONObject(YOUR JSON RESPONSE);
String s1 = object.getJSONObject("118").getString("garment_color");
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46664493

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档