我有我的JSON文件是這樣的:需要幫助的JSON解析
{
"shops": [
{
"id": "11",
"name": "Next",
"description": "Opened in 2005, offers a selection of clothes, shoes and accessories.",
"url": "/shop/550/127",
"categories": [
"4",
"33",
"34",
"16"
],
"bg_image": "/uploads/static/shop/460px/2012/12/5385-127-sale.jpg"
},
.....
我想根據使用「類別」的類別來獲取這個商店。例如,如果男士的牛仔褲從列表中點擊,則與該類別關聯的所有商店顯示爲列表。而這個JSON文件存儲在SD卡中。目前,如果點擊列表項,我可以獲取所有商店。但我無法根據類別進行過濾。
Jean.java
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import org.json.JSONObject;
import com.kabelash.sg.util.ExternalStorage;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public class Jeans extends Activity {
private final String JSON_file = "api_output_example.json";
File jsonFile;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.baby1_1);
/** Getting Cache Directory */
File cDir = ExternalStorage.getSDCacheDir(this, "json_files");
/** Getting a reference to temporary file, if created earlier */
jsonFile = new File(cDir.getPath() + "/" + JSON_file) ;
String strLine="";
StringBuilder strJson = new StringBuilder();
/** Reading contents of the temporary file, if already exists */
try {
FileReader fReader = new FileReader(jsonFile);
BufferedReader bReader = new BufferedReader(fReader);
/** Reading the contents of the file , line by line */
while((strLine=bReader.readLine()) != null ){
strJson.append(strLine);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
//System.out.println(strJson);
/** Start parsing JSON data */
new ListViewLoaderTask().execute(strJson.toString());
}
private class ListViewLoaderTask extends AsyncTask<String, Void, SimpleAdapter>{
JSONObject jObject;
/** Doing the parsing of JSON data in a non-ui thread */
@Override
protected SimpleAdapter doInBackground(String... strJson) {
try{
jObject = new JSONObject(strJson[0]);
CountryJSONParser countryJsonParser = new CountryJSONParser();
countryJsonParser.parse(jObject);
}catch(Exception e){
Log.d("JSON Exception1",e.toString());
}
CountryJSONParser countryJsonParser = new CountryJSONParser();
List<HashMap<String, String>> shops = null;
try{
/** Getting the parsed data as a List construct */
shops = countryJsonParser.parse(jObject);
}catch(Exception e){
Log.d("Exception",e.toString());
}
/** Keys used in Hashmap */
String[] from = { "shop","image","description"};
/** Ids of views in listview_layout */
int[] to = { R.id.tv_country,R.id.iv_flag,R.id.tv_country_details};
/** Instantiating an adapter to store each items
* R.layout.listview_layout defines the layout of each item
*/
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), shops, R.layout.lv_layout, from, to);
return adapter;
}
/** Invoked by the Android system on "doInBackground" is executed completely */
/** This will be executed in ui thread */
@Override
protected void onPostExecute(SimpleAdapter adapter) {
/** Getting a reference to listview of main.xml layout file */
ListView listView = (ListView) findViewById(R.id.lv_countries);
/** Setting the adapter containing the country list to listview */
listView.setAdapter(adapter);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
countryJSONParser.java
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class CountryJSONParser {
/** Receives a JSONObject and returns a list */
public List<HashMap<String,String>> parse(JSONObject jObject){
JSONArray jShops = null;
try {
/** Retrieves all the elements in the 'countries' array */
jShops = jObject.getJSONArray("shops");
} catch (JSONException e) {
e.printStackTrace();
}
/** Invoking getShops with the array of json object
* where each json object represent a country
*/
return getShops(jShops);
}
private List<HashMap<String, String>> getShops(JSONArray jShops){
int shopCount = jShops.length();
List<HashMap<String, String>> shopList = new ArrayList<HashMap<String,String>>();
HashMap<String, String> shop = null;
/** Taking each shop, parses and adds to list object */
for(int i=0; i<shopCount;i++){
try {
/** Call getShop with shop JSON object to parse the shop */
shop = getShop((JSONObject)jShops.get(i));
shopList.add(shop);
} catch (JSONException e) {
e.printStackTrace();
}
}
return shopList;
}
/** Parsing the shop JSON object */
private HashMap<String, String> getShop(JSONObject jShop){
HashMap<String, String> shop = new HashMap<String, String>();
String shopName = "";
String image="";
String description = "";
String categories = "";
//String capital = "";
try {
shopName = jShop.getString("name");
image = jShop.getString("bg_image_small");
description = jShop.getString("description");
categories = jShop.getString("categories");
String details = "Description : " + description;
//if (categories.equals("11")){
shop.put("shop", shopName);
shop.put("image", image);
shop.put("description", details);
shop.put("categories", categories);
//}
} catch (JSONException e) {
e.printStackTrace();
}
return shop;
}
}
上面給出的代碼工作正常,但我想知道如何對其進行過濾。我想用「bg_image」鏈接在圖像上顯示圖像(圖像存儲在SD卡上)。有人可以根據我的要求編輯此代碼嗎? (很遺憾,我在Google上找不到任何有用的東西)。請幫忙!
編輯︰現在我試圖做這樣的事情,但不能正確。
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class ShopJSONParser {
public class Shop {
public String name;
public String category;
public String description;
public String image;
public ArrayList<String> category_list;
}
/** Receives a JSONObject and returns a list */
public List<HashMap<String,String>> parse(JSONObject jObject){
JSONArray jShops = null;
try {
// Retrieves all the elements in the 'shops' array
jShops = jObject.getJSONArray("shops");
} catch (JSONException e) {
e.printStackTrace();
}
// Invoking getShops with the array of json object
// where each json object represent a shop
return getShops(jShops);
}
ArrayList<Shop> shops_array = new ArrayList<Shop>();
private List<HashMap<String, String>> getShops(JSONArray jShops){
int shopCount = jShops.length();
List<HashMap<String, String>> shopList = new ArrayList<HashMap<String,String>>();
HashMap<String, String> shop = null;
/** Taking each shop, parses and adds to list object */
for (int i = 0; i < jShops.length(); i++) {
JSONObject Shop_json_obj = jShops.getJSONObject(i);
Shop shop1 = new Shop();
String name = Shop_json_obj.getString("name");
String description = Shop_json_obj.getString("description");
shop1.name = name;
shop1.description = description;
shop1.category_list = new ArrayList<String>();
JSONArray categories_json_array = null;
categories_json_array = shop1.getJSONArray("categories");
for (int j = 0; j < categories_json_array.length(); j++) {
String cat = categories_json_array.getString(j);
shop1.category_list.add(cat);
}
shops_array.add(shop1);
}
return shopList;
}
/** Parsing the shop JSON object */
private HashMap<String, String> getShop(JSONObject jShop){
HashMap<String, String> shop = new HashMap<String, String>();
String shopName = "";
String image="";
String description = "";
String categories = "";
try {
shopName = jShop.getString("name");
image = jShop.getString("bg_image_small");
description = jShop.getString("description");
categories = jShop.getJSONArray("categories").toString();
System.out.println(categories);
String details = "Description : " + description;
//if (categories.equals("13")){
shop.put("shop", shopName);
shop.put("image", image);
//shop.put("description", details);
//shop.put("categories", categories);
//}
} catch (JSONException e) {
e.printStackTrace();
}
return shop;
}
}
需要進一步的幫助。謝謝。
你得到JSON數據是從您的服務器或第三方服務器? –
你有沒有考慮過使用類似GSON庫的東西? –
它在我的服務器上,但我無法修改JSON文件。 – Kabil