问题描述
我的问题是有点不同,我想,我问previously:解析JSON来cofigure android应用我有JSON从服务器来了,当我看到它在浏览器源$ C $ C;这是什么样子
My problem is little different that i thought, I ask previously:Parse JSON to cofigure android applicationI have json coming from server, When i look it in browser source code ; this is what it looks like
JOSON.config:
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:
Function to scan 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方式:
getJsonFromUrl method:
公开的JSONObject getJSONFromUrl(字符串URL){
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
AND ERROR im getting is : http://cl.ly/image/0z1u2v30341G
现在我想DOCTYPE HTML或别的东西了BCZ,但我找不到什么是错。
Now I guess its bcz of html doctype or something else, but I can not find whats wrong.
问:我怎样才能解析此JSON并将其存储在本地变量? (我没有改变服务器端的JSON的选项)在此先感谢。
Q: How can I parse this JSON and store it in local variables? (I don't have option of changing server side for json)Thanks in advance.
推荐答案
那么你可以提取子字符串,从开幕起 [,直到关闭] ,然后解析为JSON,改变你 getJSONFromUrl()
方法:
Well you can extract sub string starting from opening '[' till closing ']' and then parse it as json, make changes in your getJSONFromUrl()
method:
public void getJSONFromUrl() {
...
...
json = sb.toString().substring(html.indexOf("["), html.lastIndexOf("]") + 1);
...
}
这篇关于解析JSON是在HTML标签中的Android应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!