Android JSON解析错误无法转换为JSONArray

Android JSON解析错误无法转换为JSONArray,第1张

概述我是Android开发人员的新手,我正在尝试编写一个程序来解析网站中的一些JSON并将其输出到ListView中.但是,当我运行我的程序时,我收到错误:(有超过7行与此错误相同)03-3105:25:14.2963196-3196azilli.tenisparkE/FAILED:Jsonparsingerror:Value{“0″:”2″,”id

我是Android开发人员的新手,我正在尝试编写一个程序来解析网站中的一些JsON并将其输出到ListVIEw中.但是,当我运行我的程序时,我收到错误:(有超过7行与此错误相同)

03-31 05:25:14.296 3196-3196/nazilli.tenispark E/Failed: Json parsing
error: Value
{“0″:”2″,”ID”:”2″,”1″:”0″,”cID”:”0″,”2″:”1″,”uID”:”1″,”3″:”2017-04-01″,”date”:”2017-04-01″,”4″:”20″,”hour”:”20″}
of type org.Json.JsONObject cannot be converted to JsONArray

03-31 05:25:14.297 3196-3196/nazilli.tenispark E/Failed: Json parsing error:
Value
{“0″:”3″,”ID”:”3″,”1″:”0″,”cID”:”0″,”2″:”1″,”uID”:”1″,”3″:”2017-04-08″,”date”:”2017-04-08″,”4″:”20″,”hour”:”20″}
of type org.Json.JsONObject cannot be converted to JsONArray

我试图解析的JsON是:

{“appointments”:[{“0″:”2″,”ID”:”2″,”1″:”0″,”cID”:”0″,”2″:”1″,”uID”:”1″,”3″:”2017-04-01″,”date”:”2017-04-01″,”4″:”20″,”hour”:”20″},{“0″:”3″,”ID”:”3″,”1″:”0″,”cID”:”0″,”2″:”1″,”uID”:”1″,”3″:”2017-04-08″,”date”:”2017-04-08″,”4″:”20″,”hour”:”20″},{“0″:”4″,”ID”:”4″,”1″:”0″,”cID”:”0″,”2″:”1″,”uID”:”1″,”3″:”2017-04-15″,”date”:”2017-04-15″,”4″:”20″,”hour”:”20″},{“0″:”5″,”ID”:”5″,”1″:”0″,”cID”:”0″,”2″:”1″,”uID”:”1″,”3″:”2017-04-22″,”date”:”2017-04-22″,”4″:”20″,”hour”:”20″},{“0″:”6″,”ID”:”6″,”1″:”0″,”cID”:”0″,”2″:”1″,”uID”:”1″,”3″:”2017-03-24″,”date”:”2017-03-24″,”4″:”17″,”hour”:”17″},{“0″:”7″,”ID”:”7″,”1″:”0″,”cID”:”0″,”2″:”1″,”uID”:”1″,”3″:”2017-03-26″,”date”:”2017-03-26″,”4″:”17″,”hour”:”17″},{“0″:”8″,”ID”:”8″,”1″:”1″,”cID”:”1″,”2″:”1″,”uID”:”1″,”3″:”2017-03-26″,”date”:”2017-03-26″,”4″:”16″,”hour”:”16″},{“0″:”9″,”ID”:”9″,”1″:”2″,”cID”:”2″,”2″:”1″,”uID”:”1″,”3″:”2017-03-26″,”date”:”2017-03-26″,”4″:”15″,”hour”:”15″},{“0″:”10″,”ID”:”10″,”1″:”3″,”cID”:”3″,”2″:”1″,”uID”:”1″,”3″:”2017-03-26″,”date”:”2017-03-26″,”4″:”13″,”hour”:”13″}]}

这是我的ListvIEw自定义行java

public class adapter_appointment extends ArrayAdapter<String> {    public adapter_appointment(Context context, String[] data){        super(context, R.layout.row_layout, data);    }    @OverrIDe    public VIEw getVIEw(int position, VIEw convertVIEw, VIEwGroup parent)    {        LayoutInflater inflater = LayoutInflater.from(getContext());        VIEw customVIEw = inflater.inflate(R.layout.row_layout, parent, false);        String all_data = getItem(position);        TextVIEw Title = (TextVIEw) customVIEw.findVIEwByID(R.ID.Title);        //Title.setText(all_data.toString());        try {            JsONArray array = new JsONArray(all_data);            JsONObject obj = array.getJsONObject(0);            Log.d("SUCCESS", "JsON Object: " + obj.toString());            if (obj.has("date") && !obj.isNull("date")) {                Title.setText(obj.getString("date").toString());                Log.d("SUCCESS", "Date: " + obj.getString("date").toString());            } else {                // Do something            }        } catch (Exception e) {            Log.e("Failed", "Json parsing error: " + e.getMessage());        }        return customVIEw;    }}

这是Json.java

public class my_appointments extends AppCompatActivity {    ListVIEw lv;    inputStream is = null;    String line = null;    String result = null;    String[] data;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_my_appointments);        lv=(ListVIEw) findVIEwByID(R.ID.my_appointments);        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitNetwork().build());        //Run        getData();        ArrayAdapter adapter = new adapter_appointment(this, data);        lv.setAdapter(adapter);    }    private voID getData()    {        try {            URL url = new URL("MY URL");            httpURLConnection con = (httpURLConnection) url.openConnection();            con.setRequestMethod("GET");            is=new BufferedinputStream(con.getinputStream());            //            BufferedReader br = new BufferedReader(new inputStreamReader(is));            StringBuilder sb = new StringBuilder();            while((line=br.readline()) != null)            {                sb.append(line + "\n");            }            is.close();            result=sb.toString();            //            JsONObject jo = new JsONObject(result);            JsONArray ja = jo.getJsONArray("appointments");            data = new String[ja.length()];            for(int i=0;i<ja.length();i++)            {                jo=ja.getJsONObject(i);                data[i]=jo.toString();            }        }        catch (Exception e)        {            e.printstacktrace();        }    }}

如果我不解析:

解决方法:

您的所有数据都包含以下数据.如下所示.

 alldata={"0":"2","ID":"2","1":"0","cID":"0","2":"1","uID":"1","3":"2017-04-01","date":"2017-04-01","4":"20","hour":"20"}

所以每次你的列表项从该约会数组中获取数据.所以alldata变量将根据getItem(position)方法的位置进行更改.

所以,alldata是JsonObject.

所以你应该解析如下,

 JsONObject JsonRowData= new JsONObject(allData); try{   JsonRowData.getString("0");   JsonRowData.getString("ID"):   JsonRowData.getString("1"):   JsonRowData.getString("cID"):   JsonRowData.getString("2"):   JsonRowData.getString("uID"):   JsonRowData.getString("3");   JsonRowData.getString("date"):   JsonRowData.getString("4");    }catch(Exception e){    e.printstacktrace();   }

就像你必须打电话.希望能帮助到你

总结

以上是内存溢出为你收集整理的Android JSON解析错误无法转换为JSONArray全部内容,希望文章能够帮你解决Android JSON解析错误无法转换为JSONArray所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1118420.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存