Skip to content
Advertisement

How to get data without knowing key of json in react native

In response i get this king of data, here in data object there is various items but there key are kind of common so how can i get that data and add in my categories list.

{
   "99": "Venues",
   "100": "Party Supplies",
   "101": "Entertainment",
   "102": "Desserts",
   "103": "Catering"
}

here, how i tried to set the data in my list.

const dataItem = data.data;
const newList = [];
newList.push({item: dataItem[99]});
newList.push({item: dataItem[100]});
newList.push({item: dataItem[101]});
setList(newList);

but when i don’t have the proper name of key then how can i add it to my list? ex. sometimes i get the key like…

{
    "S99": "Venues",
    "SDF100": "Party Supplies",
    "CF101": "Entertainment",
    "VF102": "Desserts",
    "CFCV103": "Catering"
}

So, how can i set the data in list without knowing the item key.

Advertisement

Answer

You can get key and value both by using following code

for (var key in data.data) {
   console.log(key); // This will return a key, in your case it will be S99, SDF100...
   console.log(JSON_Obj[key]); // This will return value for that key.
   newList.push({item: data.data[key]});
}

from this you can add values in your list

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement