I have a URL with a string which I want to JSON.parse it (I'm new at React native).
here is part of the URL with the string -
<string>[{"Song_ID":"11","Song_Name":"The Doors - People","Song_File":"http://myurl.com/songs/The_Doors_People.mp3","Image":"http://myurl.com/images/The_Doors.jpg"},{"Song_ID":"12","Song_Name":"Smashing Pumpkins - Porcelina","Song_File":"http://myurl.com/songs/Smashing_Pumpkins_Porcelina.mp3","Image":"http://myurl.com/images/Mellon_Collie.jpg"},]</string>
Here is the code, I believe the fetch has the problem.
The dataSource: JSON.parse(responseJson)
doesn't do the job.
const URL =
"http://mobile.domain.com/site/WebService.asmx/SongsList";
export default class FetchExample extends React.Component {
static navigationOptions = {
title: "Json Data"
};
constructor(props) {
super(props);
this.state = { isLoading: true };
}
componentDidMount() {
return fetch(URL)
.then(response => response.json())
.then(responseJson => {
this.setState(
{
isLoading: false,
dataSource: JSON.parse(responseJson) // doesn't work
},
function() {}
);
})
.catch(error => {
console.error(error);
});
}
I tried dataSource: JSON.stringify(responseJson)
but it doesn't do the job also.
The render code - (I hope this part is o.k - data={this.state.dataSource}
)
render(){
if(this.state.isLoading){
return(
<View style={{flex: 1, padding: 20}}>
<ActivityIndicator/>
</View>
)
}
return(
<View style={{flex: 1, paddingTop:20}}>
<FlatList
data={this.state.dataSource}
renderItem={({item}) => <Text>{item.Song_ID}, {item.Song_Name}</Text>}
keyExtractor={({id}, index) => id} // this part with the "id" and "index" I dont understand (the index in my code is fade)
/>
</View>
);
}
}
it shows me the error: " JSON Parse error: Unrecognized token '<' ".