【发布时间】:2014-07-21 02:30:59
【问题描述】:
我正在尝试解析从 Web 服务器传回的 json 字符串,并将其转换为键控字符串数组。结果希望看起来像“str[ID] = 艺术家 - 标题”。
这是我用来获取 json 并开始解析它的代码。
new Thread(new Runnable() {
public void run() {
try {
try {
HttpClient httpclient = new DefaultHttpClient(); // Create HTTP Client
HttpGet httpget = new HttpGet("http://somesite.net/some.php"); // Set the action you want to do
HttpResponse response = httpclient.execute(httpget); // Executeit
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent(); // Create an InputStream with the response
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) // Read line by line
sb.append(line + "\n");
String resString = sb.toString(); // Result is here
JSONObject json = new JSONObject(resString);
String[] array = new Gson().fromJson(resString, String[].class);
Log.e("TESTAPP", "SOME RES E " + array.toString());
is.close(); // Close the stream
}
catch (Exception e) {
Log.e("TESTAPP", "SOME Catch Error E " + e.toString());
}
}
catch (Exception e){
Log.e("TESTAPP", "SOME Error" + e.getMessage());
}
}
}).start();
正在使用的一些示例 json
{"item":{"artist":"Billy Idol","requestid":"42207","title":"Rebel Yell"}}{"item":{"artist":"Black Sunshine","requestid":"42208","title":"Once In My Life"}}{"item":{"artist":"Blackstreet","requestid":"42209","title":"Before I Let You Go"}}{"item":{"artist":"Black Sabbath","requestid":"42210","title":"Time Machine"}}
【问题讨论】:
标签: android arrays json object