繁体   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