java - 在 Android 应用程序的标签中解析 HTML 中的 JSON

标签 java android json performance android-intent

<分区>

我的问题和我想的有点不同,我之前问过:Parse JSON to cofigure android application 我有来自服务器的 json,当我在浏览器源代码中查看它时;这是它的样子

JOSON.config:

<html>
<head></head>
<body>
<pre>
 [

    {
        "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"
    }

]

</pre>
</body>
</html>

扫描json函数:

   public void doScanAppConfigJson(){


            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 方法:

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;

} 

我得到的错误是:http://cl.ly/image/0z1u2v30341G

现在我猜它的 bcz 是 html 文档类型或其他东西,但我找不到问题所在。

问:如何解析这个 JSON 并将其存储在局部变量中? (我没有为 json 更改服务器端的选项)提前致谢。

最佳答案

好吧,您可以提取从打开 '[' 到关闭 ']' 的子字符串,然后将其解析为 json,在 getJSONFromUrl( ) 方法:

public void getJSONFromUrl() {
     ...
     ...

     json = sb.toString().substring(html.indexOf("["), html.lastIndexOf("]") + 1);

     ...
}

关于java - 在 Android 应用程序的标签中解析 HTML 中的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15649841/

相关文章:

python - 使用 Python 的 GCP 数据流。 "AttributeError: Can' t 获取模块 '_JsonSink' 上的属性 'dataflow_worker.start'

java - GSON 将数组的数组解析为 Java 对象数组

java - ajax 将序列化表单和模型列表发布到 Controller 方法

java - jetty websockets 发送二进制数据(图片)

android - 为以编程方式拍摄的照片禁用自动保存?

android - 根据其他设置在代码中构建首选项屏幕

javascript - 使用nodejs http.request()在POST/PUT json时出现带有特殊字符的无效JSON错误

java html 字体大小

java - 为什么我的自下而上合并排序在 Java 中这么慢?

java - 如何处理 Android 中的 3 个硬键