亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Parse json in Java android studio

Parse json in Java android studio

撒科打諢 2022-08-17 15:38:02
我有這個代碼來解析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 字符串???
查看完整描述

3 回答

?
牧羊人nacy

TA貢獻1862條經驗 獲得超7個贊

首先獲取 JSONObject,然后獲取其中的數組


JSONObject jsonObject = new JSONObject(jsonString);

String objectIme = jsonObject.getString("ime");

String prezime = jsonObject.getString("prezime");

上面的行將獲取整個對象,從此對象中,您可以獲得其他對象以及數組test1和test2,如下所示,然后您可以像您一樣循環該數組


    JSONArray jArray1 = new JSONArray(jsonObject.getJSONArray("test1"));

    JSONArray jArray2 = new JSONArray(jsonObject.getJSONArray("test2"));


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

                            JSON jsonObj = new JSON();

                            JSONObject mojObj = jArray1.getJSONObject(i);

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


                        }


查看完整回答
反對 回復 2022-08-17
?
慕的地6264312

TA貢獻1817條經驗 獲得超6個贊

你解析了錯誤的 json。你的 json 以 jsonObject 而不是 jsonArray 開頭。因此,在您的情況下,您必須像這樣開始(假設您的onPostExecute方法的結果變量具有json字符串)

JSONObject mojNiz = new JSONObject(result);

現在,從上面的對象中,您可以獲取json數組mojNiz


查看完整回答
反對 回復 2022-08-17
?
慕沐林林

TA貢獻2016條經驗 獲得超9個贊

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

    try {

        JSONObject jsonObject = new JSONObject(s);

        jsonObject.getString("ime");

        jsonObject.getString("prezime");


        JSONArray jsonArray = jsonObject.getJSONArray("test");


        List<Integer> list = new ArrayList<>();

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

            list.add((Integer) jsonArray.get(i));


        }


        JSONObject testObject = jsonObject.getJSONObject("test2");

        testObject.getString("1");

        testObject.getString("2");


    } catch (JSONException e) {

        e.printStackTrace();

    }


查看完整回答
反對 回復 2022-08-17
  • 3 回答
  • 0 關注
  • 130 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號