我如何解析以"/"开头的Json?在Android中使用截击? [英] How can I parse Json which starts with "/" in android using volley?

查看:106
本文介绍了我如何解析以"/"开头的Json?在Android中使用截击?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从URl解析json数据,但它以"/"开头. 我有一个数据类,它是应用程序中的POJO类. 这是我的json文件.

I am parsing json data from URl but it starts with "/". I have a data class which is a POJO class in the app. Here is my json file.

/{
    "data": [
        {
            "id": "1",
            "text": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua."
        },
        {
            "id": "2",
            "text": "Felis donec et odio pellentesque diam volutpat commodo sed. Non arcu risus quis varius quam quisque. Nibh nisl condimentum id venenatis a condimentum vitae. Vel pharetra vel turpis nunc eget. "
        },
        {
            "id": "3",
            "text": "Volutpat sed cras ornare arcu dui vivamus arcu felis bibendum. Lobortis mattis aliquam faucibus purus in. Aliquam sem fringilla ut morbi tincidunt augue interdum."
        }
    ]
}

这是我的android代码.

Here is my android code.

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, urlJsonObj, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Toast.makeText(MainActivity.this, response.toString(), Toast.LENGTH_SHORT).show();


                try {
                    Toast.makeText(MainActivity.this, "Here", Toast.LENGTH_SHORT).show();
                    Gson gson = new Gson();
                    JSONObject data1 = response.getJSONObject("data");
                    data = gson.fromJson(data1.toString(), Data.class);

                }
                catch (JSONException e){
                    Toast.makeText(MainActivity.this, "Error : "+ e.getMessage(), Toast.LENGTH_SHORT).show();
                }
                dataList.add(data);
                mAdapter = new ViewPagerAdapter(dataList,layoutInflater, viewPager2);

                progressBar.setVisibility(View.GONE);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
                progressBar.setVisibility(View.GONE);
            }
        });
        requestQueue.add(jsonObjectRequest);

我收到错误消息字符0的预期文字值.

I am getting an error "Expected literal value at character 0.

推荐答案

只需执行以下操作即可从响应中删除"/":

just remove the "/" from the response by doing:

String subString = (respnse.tostring).replace("/","");
JSONobject jsonObj = new JSONobject(subString);
Gson gson = new Gson();
JSONObject data1 = jsonObj.getJSONObject("data");
data = gson.fromJson(data1.toString(), Data.class);

希望这会有所帮助

这篇关于我如何解析以"/"开头的Json?在Android中使用截击?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆