簡體   English   中英

在Java Android Studio中解析JSON

[英]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字符串?

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

JSONObject mojNiz = new JSONObject(result);

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

首先獲取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));

                        }
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();
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM