我正在制作一个应用程序,其中我将json数据提取到listview,但我不知道如何使用json解析器将数据添加到listview中时以编程方式添加$符号,这里我放置我的代码: - < / p>
@Override
protected ArrayList<HashMap<String, String>> doInBackground(String... params) {
HttpClient client = new DefaultHttpClient();
// Perform a GET request to a JSON list of all the videos by a specific user
HttpUriRequest request = new HttpGet(URL);
// Get the response that sends back
HttpResponse response = null;
try {
response = client.execute(request);
} catch (ClientProtocolException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Convert this response into a readable string
String jsonString = null;
try {
jsonString = StreamUtils.convertToString(response.getEntity().getContent());
} catch (IllegalStateException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Create a JSON object that we can use from the String
JSONObject json = null;
try {
json = new JSONObject(jsonString);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try{
JSONArray jsonArray = json.getJSONArray(KEY_CATEGORY);
for(int i=0;i < jsonArray.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject jsonObject = jsonArray.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put(KEY_TITLE, jsonObject.getString(KEY_TITLE));
map.put(KEY_DESCRIPTION, jsonObject.getString(KEY_DESCRIPTION));
map.put(KEY_COST, jsonObject.getString(KEY_COST));
map.put(KEY_THUMB_URL, jsonObject.getString(KEY_THUMB_URL));
itemsList.add(map);
}
return itemsList;
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
return null;
}
JSON数据: -
{
"pizza":[
{
"title" : "Pizza Item 1",
"description" : "Lorem ipsum dolor sit amet, consectetur adipisicing elit",
"cost" : "10.00",
"imageUri" : "http://www.pizzahut.com.au/images/menu/Pizza-Lge-BBQ_Meatlovers.png"
},
{
"title" : "Pizza Item 2",
"description" : "Lorem ipsum dolor sit amet, consectetur adipisicing elit",
"cost" : "9.00",
"imageUri" : "http://www.pizzahut.com.au/images/menu/Pizza-Lge-BBQ_Meatlovers.png"
}
.........
答案 0 :(得分:0)
只需将其附加到Map值即可。
map.put(KEY_COST, jsonObject.getString(KEY_COST)+"$");