解析JSON到cofigure android应用JSON、cofigure、android

2023-09-05 01:22:11 作者:Taurus-安于此生

在我的Andr​​oid应用程序,我不得不使用JSON从服务器做出具体调整应用程序。 我想才达到的阅读本JSON和所有的值存储到本地变量在应用程序中执行操作。

In my android app, I have to use JSON from server to make specific adjustment in the app. I'm trying to achive is to read this json and store all the values into local variables to perform actions inside the app.

JSON从服务器:

[

    {
        "sett": " ",
        "glHdr": {
            "sm": [ ],
            "scleHPad": false,
            "st": "sbsm"
        },
        "colrBG": [
            23,
            105,
            184,
            100
        ],
        "colrTB": [
            0,
            0,
            0,
            0
        ],
        "colrTR": [
            255,
            255,
            255,
            100
        ],
        "glFtr": {
            "icoSz": "icoSzN",
            "sm": [ ],
            "scleHPad": false,
            "gvNR": 3,
            "gvHIT": false,
            "gvNC": 3,
            "st": "igsm"
        },
        "statBr": true
    },
    {
        "sm": [
            {
                "tbico": "b43-jeep.png",
                "t": "Welcome!",
                "w": "http://google.com/start",
                "st": "w",
                "wTBL": "wTBLN"
            },
            {
                "t": "Content screen title",
                "f": "Eltec%20Spec%20sheet%20Gim%20RD30W.pdf",
                "st": "f"
            },
            {
                "tbico": "109-chicken.png",
                "t": "Sub menu",
                "sm": [
                    {
                        "t": "Screen 1",
                        "st": "f"
                    },
                    {
                        "t": "Screen 2",
                        "w": "Http://google.com",
                        "st": "w",
                        "wTBL": "wTBLT"
                    }
                ],
                "st": "sm"
            },
            {
                "st": "f"
            }
        ],
        "st": "tbm"
    }

]

要分析此我创建解析JSON类,并试图想出方法来读取,这JSON在我的应用程序商店的价值。

To parse This I have created Parse JSON class, and trying to come up with method to read and store value of this json in my app.

功能做到这一点:

public void doScanAppConfigJson(){

private static final String = TAG_TITLE;
private static final String = TAG_WEB_ADDRESS;
private static final String = TAG_SCREEN_TYPE;
private static final String = TAG_FILENAME;


        JSONArray appConfig = null;

        // Function for looping json object via ParseJson class.
        //Creating JSON Parser instance
        JSONParser jParser = new JSONParser();

        //Getting json strings from url
        JSONObject jsonObject = jParser.getJSONFromUrl(url);

    try{
        //Getting array of settings
        appConfig = jsonObject.getJSONArray(ConfigConstants.TABLE_VIEW_SUB_MENU_CONFIG);
        //loop throw all the objects under -sm[]
        for (int i = 0; i < appConfig.length(); i++){

            JSONObject sm = appConfig.getJSONObject(i);

            //Now store each of this json in local constant var.

            String tabTitle = sm.getString(TAG_TITLE);

            String webAddress = sm.getString(TAG_WEB_ADDRESS);

            String screenType = sm.getString(TAG_SCREEN_TYPE);

            String fileName = sm.getString(TAG_FILENAME);

        }

    }catch (JSONException e){
        e.printStackTrace();

    }

    }

getJSONFromUrl方法:

getJSONFromUrl Method:

public JSONObject getJSONFromUrl(String url) {

        //Global authentication for link username and password.
        Authenticator.setDefault(new Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication(){

            return new PasswordAuthentication("username", "password".toCharArray());   
            }

        });

        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();           

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }

和即时得到错误: settingsjava.lang.NullPointException 这个,但我有明确的VAR,请有人告诉我,我该如何解析上面的JSON。我是一个正确的轨道上实现答上。我的问题?

And Im getting errors: settingsjava.lang.NullPointException on this but i have define vars, Please some one tell me how do i parse above JSON. Am i on a right track to achieve ans. of my question?

推荐答案

目前JSON字符串格式为:

Current json string format is :

[  //<<<< JSONArray

    {   //<<<< JSONObject
       // other items here...
    }

]

这JSON字符串内容 JSONArray 的根元素,而不是的JSONObject 。你需要先将其转换成JSONArray然后从中提取的JSONObject:

this Json string content JSONArray as root element instead of JSONObject. you will need to convert it first JSONArray then extract JSONObject from it :

JSONArray jsonObject = jParser.getJSONFromUrl(url);

也改变 getJSONFromUrl 方法的返回类型为 JSONArray

also change getJSONFromUrl method return type to JSONArray

 
精彩推荐
图片推荐