JsonArray解析
你肯定有在文件末尾没有空行?就像这样:
[
{
code: "UNLC",
cours_jour: "40 020",
variation: "0.00"
},
{
code: "UNXC",
cours_jour: "7 450",
variation: "0.00"
}
]
<-- Empty line here!
你的JSON对象字段需要用双引号
IE
{
"code": "BOAC",
"cours_jour": "29 000",
"variation": "-1.69"
}
封装如何生成的JSON文件?
- 编辑
您可以使用下面的代码的网页下载到一个字符串,然后将其转换为一个JSONArray再拉各的JSONObject。您不能运行在主线程中的任何Web请求,因此可以扩展一个新的AsyncTask或线程或可运行来执行以下
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpost = new HttpPost("http://www.impaxis-securities.com/securities/cours-actions/cours.json");
httpost.setHeader("Accept", "application/json");
httpost.setHeader("Content-type", "application/json");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String response;
try {
response = httpclient.execute(httpost, responseHandler);
JSONArray arr = new JSONArray(response);
int arrLength = arr.length();
if(arrLength > 0)
{
for(int i = 0; i < arrLength; i++)
{
JSONObject item = arr.getJSONObject(i);
String code = item.getString("code");
String cours_jour = item.getString("cours_jour");
String variation = item.getString("variation");
//Either insert to a DB or add to an array list and return it
}
}
}
catch (ClientProtocolException e) {
//Issue with web server
}
catch (IOException e) {
//Issue with request
}
catch (JSONException e) {
//ISSUE Parsing JSON from site
}
---编辑
我测试的代码,它看起来像有一个错误使用JSON插件/ REST服务
json文件是由drupal模块生成的..我并不真正知道模块的名称,但是我使用了他们给我的json文件.. – 2012-08-17 18:17:11
听起来像模块创建的数组对象不会将引用围绕它的键.IE $ a = array(“code”=>“$ code”,“cours_jour”=>“$ cours_jour”,“variation”=>“$ variation”);
json_encode($ a); 但他们正在做 $ a = array(code =>“$ code”,cours_jour =>“$ cours_jour”,variation =>“$ variation”); –
ainesophaur
2012-08-17 18:36:00
嗨。你真的得到了代码,cours_jours和变化。我做System.out.println(response)int try try,但我没有任何东西 – 2012-08-18 13:34:48
后的相对JSON解析代码,然后我们会帮助ü更 – 2012-08-17 17:43:46
'人品0 of'中输入什么的终结?你错过了会告诉我们问题出在哪里的那部分错误。我期待它能说出类似于“____”的内容。 – Izkata 2012-08-17 17:55:19