0

I have following JSON string :

{"GetActiveOrdersResult":"[

{ \"id\":1,\"Order_Number\":\"X1\" }
{ \"id\":2,\"Order_Number\":\"X2\" }
{ \"id\":3,\"Order_Number\":\"X3\" }

]"}

and i want to show my order numbers in flat list: X1 X2 X3

My Fetch function:

fetch('myURL',{ method: 'get' })
.then(response => {
if (response.status === 200) {
return response.json();
} else {
throw new Error('Something went wrong on api server!');
    }
  })
  .then(json => {
    
    setData(json.GetActiveOrdersResult);
   // console.log(json);
  })
  .catch(error => {
   console.error(error);
  });
  });

Here is the code of Flatlist

<FlatList
data={data}
renderItem={({ item }) => <Text>{item.Order_Number}</Text>}
keyExtractor={item => item.id.toString()}
/>
  • Is there a specific reason you do not [JSON.parse](https://www.w3schools.com/js/js_json_parse.asp) that `json.GetActiveOrdersResult` before persisting it to the state? – vahdet Jul 07 '20 at 11:47
  • oh thank you so, much I've tried this thing `var obj = JSON.parse(json.GetActiveOrdersResult);` and remove `.toString()` from this line `keyExtractor={item => item.id.toString()` and have my required answer – Sheikh Babar Qadri Jul 07 '20 at 11:56
  • 1
    Ah ok. Then I think it is a dupe, after all: [Safely turning a JSON string into an object](https://stackoverflow.com/questions/45015/safely-turning-a-json-string-into-an-object) – vahdet Jul 07 '20 at 11:58

0 Answers0