我正在尝试以下列特定格式获取一个json文件。
{
"business":[
{
"id":"13",
"category":"Dinner",
"subcategory":"",
"name_eng":"dinner 1",
"name_arab":"dinner 1",
"mobile":"12345",
"address":"not now",
"logo":"1.gif",
"contact":"Call",
"open_time":"10 PM",
"close_time":"8 PM"
}
],
"business_cat":[
[
"cat 1",
{
"name":"dish 1",
"id":"7",
"name_arab":"dish1",
"desc_eng":"5 Chicken Legs\r\n5 Hamous\r\n5 Fires\r\n5 Pepsi",
"desc_arab":"5 Chicken Legs\r\n5 Hamous\r\n5 Fires\r\n5 Pepsi",
"price":"200 KD",
"logo":"laptop.jpeg"
},
{
"name":"dish 2",
"id":"8",
"name_arab":"dish 2",
"desc_eng":"5 Chicken Legs\r\n5 Hamous\r\n5 Fires\r\n5 Pepsi",
"desc_arab":"5 Chicken Legs\r\n5 Hamous\r\n5 Fires\r\n5 Pepsi",
"price":"123789",
"logo":"micky.jpg"
},
{
"name":"dish3",
"id":"13",
"name_arab":"dish3",
"desc_eng":"5 Chicken Legs\r\n5 Hamous\r\n5 Fires\r\n5 Pepsi",
"desc_arab":"5 Chicken Legs\r\n5 Hamous\r\n5 Fires\r\n5 Pepsi",
"price":"300 KD",
"logo":"1.jpg"
},
{
"name":"new dish",
"id":"15",
"name_arab":"new dish",
"desc_eng":"5 Chicken Legs\r\n5 Hamous\r\n5 Fires\r\n5 Pepsi",
"desc_arab":"5 Chicken Legs\r\n5 Hamous\r\n5 Fires\r\n5 Pepsi",
"price":"213 KD",
"logo":"12.jpg"
}
],
[
"cat2",
{
"name":"dish3",
"id":"14",
"name_arab":"dish3",
"desc_eng":"5 Chicken Legs\r\n5 Hamous\r\n5 Fires\r\n5 Pepsi",
"desc_arab":"5 Chicken Legs\r\n5 Hamous\r\n5 Fires\r\n5 Pepsi",
"price":"123 KD",
"logo":"10.jpg"
},
{
"name":"dish 4",
"id":"16",
"name_arab":"dish4",
"desc_eng":"5 Chicken Legs\r\n5 Hamous\r\n5 Fires\r\n5 Pepsi",
"desc_arab":"5 Chicken Legs\r\n5 Hamous\r\n5 Fires\r\n5 Pepsi",
"price":"110 KD",
"logo":"3.jpg"
}
]
]
}
现在我如何访问“business”,“business_cat”和“business_cat”“cat 1”和“cat 2”名称以及“cat 1”和“cat 2”中的所有对象。对于我来说处理这样的json数据有点复杂,请帮帮我。
答案 0 :(得分:2)
试试如下:
使用Below方法读取JSON:
public static String parseJSON(String p_url) { JSONObject jsonObject = null; String json = null; try { // Create a new HTTP Client DefaultHttpClient defaultClient = new DefaultHttpClient(); // Setup the get request HttpGet httpGetRequest = new HttpGet(PeakAboo.EmailUrl + p_url); System.out.println("Request URL--->" + PeakAboo.EmailUrl + p_url); // Execute the request in the client HttpResponse httpResponse = defaultClient.execute(httpGetRequest); // Grab the response BufferedReader reader = new BufferedReader(new InputStreamReader( httpResponse.getEntity().getContent(), "UTF-8")); json = reader.readLine(); System.err.println("JSON Response--->" + json); // Instantiate a JSON object from the request response jsonObject = new JSONObject(json); } catch (Exception e) { // In your production code handle any errors and catch the // individual exceptions e.printStackTrace(); } return json; }
之后解析如下:
try{ String response=parseJSON(URL); JSONObject responseJson = new JSONObject(response); JSONArray m_bus_cat=responseJson.getJSONArray("business_cat"); for(int i=0; i<m_bus_cat.length(); i++){ JSONObject catJson = new JSONObject(i); if(catJson .has("cat 1")){ JSONArray resultArr = resultInstanceJson.getJSONArray("cat 1"); for(int j=0; j<resultArr.length(); j++){ String Name = catJson .getString("name"); String id=catJson.getString("id"); } } } } } }catch(Exception e){ e.printStackTrace(); }
答案 1 :(得分:0)
尝试使用以下代码来解析响应数据
此处jsonString
是您从回复中获得的字符串。
try {
JSONObject jsonObject=new JSONObject(jsonString);
JSONArray business=jsonObject.getJSONArray("business");
for(int i=0;i<business.length();i++){
JSONObject businessObject=business.getJSONObject(i);
//you can store in array or arraylist
String id=businessObject.getString("id");
String category=businessObject.getString("category");
// and so on
}
JSONArray businessCat=jsonObject.getJSONArray("business_cat");
for(int i=0;i<businessCat.length();i++){
JSONArray businessCatInner=businessCat.getJSONArray(i);
String cat=businessCatInner.getString(0);
for(int j=0;j<businessCatInner.length();j++){
JSONObject businessInnerObject=businessCatInner.getJSONObject(i);
//you can store in array or arraylist
String name=businessInnerObject.getString("name");
String name_arab=businessInnerObject.getString("name_arab");
// and so on
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}