我是一名 php 程序员,我正在努力学习 JAVA。我在尝试解析 json 文件时遇到问题。已经3天了..对此感到头疼。我正在创建一个 android 应用程序(使用 JAVA)来解析 json 文件。这是示例 json 数据(http://metalgearrisingguide.com/samplejson.json)以及如何显示图像?
当我尝试时,我的 logcat 出现以下错误,模拟器上没有显示任何内容
Error parsing data org.json.JSONException: Value {"product":[{"summary":"Speak a word and the knob will turn by itself","state":"good","image":"http:\/\/upload.wikimedia.org\/wikipedia\/commons\/e\/e9\/Door_Knob_with_Lock_USA.jpg","title":"Special Door knob requires no holding just music","address":[{"address":"Merchant address 1"},{"address":"merchant address 2"}],"url":"http:\/\/merchantwebsite.com"},{"summary":"This door bell will detect faces of known people and change music if unknown","state":"good","image":"http:\/\/www.thedoorbell.net\/images\/DoorbellHeader.jpg","title":"Special Door Bell that uses face recognition technology","address":[{"address":"Merchant address 1"},{"address":"merchant address 2"}],"url":"http:\/\/merchantwebsite2.com"},{"summary":"Can't wake up in the morning?\r\nWill you be awake for party? Listen to your favourite song that will awake you!","state":"refurnished","image":"http:\/\/jgrundig.wpengine.netdna-cdn.com\/wp-content\/uploads\/2011\/08\/RCA-Alarm-Clock-with-Charging-Cord-RC107.jpg","title":"The only alarm clock that plays your favourite song from your smartphone","address":[{"address":"Merchant address 1"},{"address":"merchant address 2"}],"url":"http:\/\/merchantwebsite3.com"}],"site":{"sitename":"Simple Product Find","imgprefix":"http:\/\/mydomain.com\/","urlprefix":"http:\/\/mydomain.com\/"}} at data of type org.json.JSONObject cannot be converted to JSONArray
这是我的 JSONfunctions 类:
public class JSONfunctions {
public static JSONObject getJSONfromURL(String url){
InputStream is = null;
String result = "";
JSONObject jArray = null;
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
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();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
try{
jArray = new JSONObject(result);
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return jArray;
//return result;
}
}
这是我的代码:
public class Main extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
JSONObject json = JSONfunctions.getJSONfromURL("http://metalgearrisingguide.com/samplejson.json");
try{
JSONArray data = json.getJSONArray("data");
JSONArray products = data.getJSONArray(1);
for(int i=0;i<products.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = products.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("title", "Product name:" + e.getString("title"));
map.put("state", "Condition:" + e.getString("state"));
map.put("summary", "Description: " + e.getString("summary"));
mylist.add(map);
Set set = map.entrySet();
// Get an iterator
Iterator o = set.iterator();
// Display elements
while(o.hasNext()) {
Map.Entry me = (Map.Entry)o.next();
System.out.print(me.getKey() + ": ");
System.out.println(me.getValue());
}
System.out.println();
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main,
new String[] { "state", "summary" },
new int[] { R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
Toast.makeText(Main.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show();
}
});
}
}