Parse json in Java android studio

我有这个代码来解析Java中的json,但问题是我的json看起来像这样


{"ime":"Alen","prezime":"Osmanagi\u0107","test":[1,2,3,4,5],"test2":{"1":"test","2":"555","test":"888","om":"hasd"}}

我的Java代码用于解析看起来像这样:


protected void onPostExecute(String result) {

        pDialog.dismiss();

        runOnUiThread(new Runnable() {

            public void run() {


                ListView listView =(ListView)findViewById(R.id.jsonList);

                if ( true) {

                    try {

                       JSONArray mojNiz = response.getJSONArray("");


                        List<JSON> noviJSON = new ArrayList<>();

                        //Popuniti podacima

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

                            JSON jsonObj = new JSON();

                            JSONObject mojObj = mojNiz.getJSONObject(i);

                            jsonObj.setIme(mojObj.getString(KEY_NAME));

                           // jsonObj.setPrezime(mojObj.getString(KEY_DOB));

                          //jsonObj.setPrezime(mojObj.getString(KEY_DESIGNATION));

                            noviJSON.add(jsonObj);

                        }


                        adapter = new Adapter(noviJSON, getApplicationContext());


                        listView.setAdapter(adapter);


                    } catch (JSONException e) {

                        e.printStackTrace();

                    }

                } else {

                    Toast.makeText(MainActivity.this,

                            "Problem u loadiranjuz podataka",

                            Toast.LENGTH_LONG).show();


                }

如何解析此特定的 json 字符串???


撒科打诨
浏览 111回答 3
3回答

牧羊人nacy

首先获取 JSONObject,然后获取其中的数组JSONObject jsonObject = new JSONObject(jsonString);String objectIme = jsonObject.getString("ime");String prezime = jsonObject.getString("prezime");上面的行将获取整个对象,从此对象中,您可以获得其他对象以及数组test1和test2,如下所示,然后您可以像您一样循环该数组&nbsp; &nbsp; JSONArray jArray1 = new JSONArray(jsonObject.getJSONArray("test1"));&nbsp; &nbsp; JSONArray jArray2 = new JSONArray(jsonObject.getJSONArray("test2"));&nbsp;for (int i = 0; i < jArray1 .length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSON jsonObj = new JSON();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; JSONObject mojObj = jArray1.getJSONObject(i);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; jsonObj.setIme(mojObj.getString(KEY_NAME));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }

慕的地6264312

你解析了错误的 json。你的 json 以 jsonObject 而不是 jsonArray 开头。因此,在您的情况下,您必须像这样开始(假设您的onPostExecute方法的结果变量具有json字符串)JSONObject&nbsp;mojNiz&nbsp;=&nbsp;new&nbsp;JSONObject(result);现在,从上面的对象中,您可以获取json数组mojNiz

慕沐林林

String s = "{\"ime\":\"Alen\",\"prezime\":\"Osmanagi\\u0107\",\"test\":[1,2,3,4,5],\"test2\":{\"1\":\"test\",\"2\":\"555\",\"test\":\"888\",\"om\":\"hasd\"}}";&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; JSONObject jsonObject = new JSONObject(s);&nbsp; &nbsp; &nbsp; &nbsp; jsonObject.getString("ime");&nbsp; &nbsp; &nbsp; &nbsp; jsonObject.getString("prezime");&nbsp; &nbsp; &nbsp; &nbsp; JSONArray jsonArray = jsonObject.getJSONArray("test");&nbsp; &nbsp; &nbsp; &nbsp; List<Integer> list = new ArrayList<>();&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < jsonArray.length(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; list.add((Integer) jsonArray.get(i));&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; JSONObject testObject = jsonObject.getJSONObject("test2");&nbsp; &nbsp; &nbsp; &nbsp; testObject.getString("1");&nbsp; &nbsp; &nbsp; &nbsp; testObject.getString("2");&nbsp; &nbsp; } catch (JSONException e) {&nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java