我是本地编程的新手。我发现更难的是访问react-native中的对象数组。我正在开发一个简单的react-native应用程序,我已经发出了localhost服务器请求但是在服务器响应中,我得到了对象数组。我尝试使用普通的JavaScript周期运算符访问该对象的数组。但是,我不允许我们访问这些对象。
在这里,我使用localhost服务器请求
try {
JSONObject jsonObject = new JSONObject(s);
JSONArray posts = jsonObject.getJSONArray("posts");
for (int i = 0; i < posts.length(); i++) {
JSONObject fileObj = posts.getJSONObject(i);
Data_SAerver item = new Data_SAerver(
fileObj.getString("firstname"),
fileObj.getString("created_at"),
fileObj.getString("post_desc"));
// Get files array
JSONArray files = fileObj.getJSONArray("files");
// Create a List for storing the filenames
List<String> fileNameArray = new ArrayList<>();
// Iterate the files array
for (int j = 0; j < files.length(); j++) {
JSONObject fileNameObj = files.getJSONObject(i);
// Get file_name string
String fileName = fileNameObj.getString("file_name");
// Add the filename to the filename array
fileNameArray.add(fileName);
}
// Store it in the model object. You will need to create this field in Data_SAerver class
item.setFileNames(fileNameArray);
data_sAervers.add(item);
}
} catch (JSONException e) {
e.printStackTrace();
}
在react-native render()函数中,我试图访问服务器响应数据中的那些对象数组。在这里,我收到以下错误,如下图所示。
请帮我找到一个解决方案来访问react-native中的那些对象属性数组。提前谢谢......
答案 0 :(得分:2)
应该运行。首先检查状态内是否准备好响应。 如果您的回复准备就绪,您可以更改状态。检查代码。
constructor(props){
super(props);
this.state = {
data: [],
isReady:false, //create isReady on the state
}
componentWillMount(){
fetch('http://139.162.27.183:8080/Eshiksha/org/'+this.props.orgId+'/branch/'+this.props.branchId+'/videourl/videourls', { 'method' : 'GET',
headers: {
'Content-Type' : 'application/json',
'Authorization' : this.props.auth
}
})
.then((response) => response.json())
.then((responseData) => {
this.setState({ data: responseData, isReady:true}); //when response came change the status.
})
.done();
}
render(){
const {isReady} = this.state; //check the state if response is ready render your scrollview
if(isReady)
return(
<ScrollView>
<View style={styles.container}>
<View style={styles.card}>
<View style={styles.button}>
<Text style={styles.buttonText}>{this.state.data.videoUrls[4].eventName}</Text> // Here, I'm getting error which is shown below image
</View>
<TouchableOpacity>
<View style={styles.button}>
<Image style={styles.image} source={require('./school.jpg')} />
</View>
</TouchableOpacity>
</View>
</View>
</ScrollView>
);
return(
<View/>
)
}
答案 1 :(得分:0)
如果您要使用它来显示Rows中的数据,您可以执行以下操作:
javax.crypto.BadPaddingException: Decryption error
at sun.security.rsa.RSAPadding.unpadV15(RSAPadding.java:380) ~[na:1.8.0_131]
at sun.security.rsa.RSAPadding.unpad(RSAPadding.java:291) ~[na:1.8.0_131]
at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:356) ~[sunjce_provider.jar:1.8.0_112]
at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:389) ~[sunjce_provider.jar:1.8.0_112]
at javax.crypto.Cipher.doFinal(Cipher.java:2048) ~[na:1.8.0_121]
...
constructor(props) {
super();
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
this.state = {
dataSource: ds.cloneWithRows([
''
]),
}
}
如果您只想保存数组以执行其他操作
.then((responseData) => {
var arrayOutput=[];
arrayOutput.push(responseData);
this.setState({
dataSource: this.state.dataSource.cloneWithRows(arrayOutput),
});
})
然后数组将保存在this.state.saveArray上,您可以在需要时使用它。