2

Possible Duplicate:
Reading a Json Array in android

I trying to get the field name from the string using JSON. I managed to get the value but how to get the field. For an example:

String strjson:

{
    "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"
                        }
                    }
                ]
            }
        }
    ]
}

I managed to get the value by doing this.

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"));                        
}

How do i get the field name. Let say i want "Product Name","Type", "Grade" and so on

Community
  • 1
  • 1
chinna_82
  • 6,353
  • 17
  • 79
  • 134
  • @RC. I dont get any error... I want to get different field name instead the value since i managed to get the value... – chinna_82 Aug 10 '12 at 06:37

1 Answers1

1

You can get all attribute names through .names() which returns an JSONArray. With the help of the array, you can get the appropriate attribute name of a given value (assuming the value is unique).

Ingo
  • 429
  • 3
  • 7