问题描述
我们正在将一个值从 javascript 发送到一个 json 文件——这是可行的;但是,该值被放入了 json 文件的错误位置。
<KeyboardAvoidingView
style={styles.kbContainer}
behavior={Platform.OS === 'ios' ? 'padding' : null}
>
<SafeAreaView style={styles.parentContainer}>
<NavHeader navProps={navProps} /> // Imported custom Header component here
<ScrollView>
{other content}
</ScrollView>
</SafeAreaView>
</KeyboardAvoidingView>
当我们尝试时,它要么被添加在整个“球体位置”部分之前或之后——不过,我们希望在“球体位置”的 [] 内发送编号。
这是我们的代码:
[{"sphereposition":[]}]
感谢您的帮助!
解决方法
要获取插入到嵌套数组中的值,您应该解决该属性:
json[0].sphereposition.push( 0,xcoord );
// ^^^^^^^^^^^^^^^^^^
附带说明:不要调用该变量 json
,因为它实际上是一个数组对象,而不是 JSON 字符串。当你用字符串化的版本覆盖它时,它就会变成一个 JSON 字符串。但最好为此使用单独的变量:
var arr = JSON.parse( data );
arr[0].sphereposition.push( 0,xcoord );
var json = JSON.stringify( arr );