我在React Native中有一个代码。我用它将数组传递给本机模块,我想在本机模块上处理它,ReadableArray
的示例:
{"AuditScheduleDetailID":5525,"AuditAnswerId":33,"LocalFindingID":1,"LocalMediaID":1,"ExtFiles":"jpg"}
我想获取密钥的每个值-我该怎么做?
我已经尝试过this article,但我不太了解。
我只是现在这样硬编码:
@ReactMethod
public void insert(ReadableArray name, String file) {
DatabaseHandler db = new DatabaseHandler(getReactApplicationContext());
String a = "path to file";
for(int i=0 ; i<name.size(); i++){
//expected result
int audit = 5525 //from AuditScheduleDetailID value
int answer = 33 // from AuditAnswerId value
int finding = 1 // from LocalFindingID value
int media = 1 // from LocalMediaID value
String exten = "jpg" // from ExtFiles value
db.addContact(new Contact(audit, answer, finding, media, exten));
}
}
有没有办法从readarray
value
获取每个key
? 最佳答案
在javascript上,您调用此方法只是作为对象传递
import { NativeModules } from 'react-native';
NativeModules.<<ModuleName>>.insert({"AuditScheduleDetailID":5525,"AuditAnswerId":33,"LocalFindingID":1,"LocalMediaID":1,"ExtFiles":"jpg"}, <<fileName>>);
修改您的react方法以接受
ReadableMap
而不是ReadableArray
@ReactMethod
public void insert(ReadableMap readableMap, String file) {
String auditScheduleDetailID = readableMap.getString("AuditScheduleDetailID");
String auditAnswerId = readableMap.getString("AuditAnswerId");
String localFindingID = readableMap.getString("LocalFindingID");
String localMediaID = readableMap.getString("LocalMediaID");
String extFiles = readableMap.getString("ExtFiles");
//your logic
}