可能重复:
在 android 中读取 Json 数组
我尝试使用 JSON 从字符串中获取字段名称。我设法获得了价值,但如何获得该领域。例如:
字符串字符串:
{
"IndustryDetails": [
{
"industryId": "1",
"industryName": "Bird Nest",
"productProfilePhoto": "",
"industryIcon": "",
"industryFields": {
"items": [
{
"selectionItem": {
"Product Name": "燕碎 - YanShu ",
"Type": "盏片 - pcs",
"Grade": "碎片 - Broken",
"Farm": "DVS-EBN-000003",
"Made in": "Malaysia",
"Manufactured Date": "2012-08-09",
"Expiry Date": "2012-08-09",
"Export Permit": "1234",
"Import Permit": "9999"
}
}
]
}
}
]
}
通过这样做,我设法获得了价值。
JSONObject json_data = new JSONObject(strjson);
JSONArray nameArray = json_data.names();
JSONArray valArray = json_data.toJSONArray(nameArray).getJSONArray(0);
Log.i(MainActivity.class.getName(),
"Number of entries " + valArray.length());
for (int i = 0; i < valArray.length(); i++) {
JSONObject jsonObject = valArray.getJSONObject(i);
Log.i(MainActivity.class.getName(), " industryId = "+jsonObject.getString("industryId"));
Log.i(MainActivity.class.getName(), " industryName = "+jsonObject.getString("industryName"));
Log.i(MainActivity.class.getName(), " industryStatus = "+jsonObject.getString("productProfilePhoto"));
Log.i(MainActivity.class.getName(), " industryDescription = "+jsonObject.getString("industryIcon"));
Log.i(MainActivity.class.getName(), " industryIconFilepath = "+jsonObject.getString("industryFields"));
}
我如何获得字段名称。假设我想要“产品名称”、“类型”、“等级”等