Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Android应用程序中解析HTML in标记中的JSON_Java_Android_Json_Performance_Android Intent - Fatal编程技术网

Java 在Android应用程序中解析HTML in标记中的JSON

Java 在Android应用程序中解析HTML in标记中的JSON,java,android,json,performance,android-intent,Java,Android,Json,Performance,Android Intent,我的问题与我之前想的没什么不同,我问: 当我在浏览器源代码中查看json时,它来自服务器;这就是它看起来的样子 JOSON.config: <html> <head></head> <body> <pre> [ { "sett": " ", "glHdr": { "sm": [ ], "scleHPad": false,

我的问题与我之前想的没什么不同,我问: 当我在浏览器源代码中查看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>
我得到的错误是:

现在我猜它的bcz的html doctype或其他东西,但我找不到有什么错


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

您可以从打开“[”到关闭“]”提取子字符串,然后将其解析为json,在
getJSONFromUrl()方法中进行更改:


如果您可以将服务器配置为停止在html标记中包装它,那将是理想的。@SamDufel:我没有更改服务器端代码的选项!我可以在应用程序中做些什么来避免这个标签并读取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();

        }

        }
    //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;

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

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

     ...
}