简体   繁体   中英

Parse json in Java android studio

I have this code to parse a json in Java, but problem is my json looks like this

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

And my java code for parsing looks like :

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

                }

How can I parse this particular json string ???

You are parsing your json wrong. Your json starts with jsonObject instead of jsonArray. So in your case you have to start like this (assuming that your result variable of onPostExecute method has the json string)

JSONObject mojNiz = new JSONObject(result);

Now from the above mojNiz object you can get your json array

First get the JSONObject and then the array inside it

JSONObject jsonObject = new JSONObject(jsonString);
String objectIme = jsonObject.getString("ime");
String prezime = jsonObject.getString("prezime");

The above line will get the whole object and from this object you can get other objects and the array test1 and test2 like below then you can loop through that array like you did

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM