[英]pass a javascript array to java array using json
我正在尝试使用JSON将javascript数组项传递给java服务器。 我的服务器收到以下字符串:
[
{"attr1":"SomeValue1","attr2":"SomeValue2"},
{"attr1":"SomeValue3","attr2":"SomeValue4"}
]
我正在尝试使用JsonArray
,但我可能做错了(我不在这里添加我的代码,因为它可能只是愚蠢的)。
谁能给我正确的方法来创建迭代我的String中的值?
编辑:根据要求,我的愚蠢的代码:
jsnobject = new JSONObject(items); //items is the string described above
JSONArray jsonArray = jsnobject.getJSONArray("");
if(jsonArray != null){
for(int i=0 ; i<jsonArray.length();i++){
JSONObject explrObject = jsonArray.getJSONObject(i);
System.out.println("name = "+explrObject.get("fileName"));
}
}
我从来没有使用过JsonArray(之前我曾经使用过gson来介绍json和Java),但是看看文档
看起来你可以通过将正确的json字符串传递给constuctor来创建JsonArray。 然后你应该像典型的数组一样遍历它。
JsonArray myArray = new JsonArray(jsonString);
int length = myArray.length();
for(int i=0; i<length; i++){
myArray.get(i)
//note this returns an object of type object,
//use other get functions to get other types
}
JSON.stringify(array)
没有你的代码很难看到你想要的东西,但这就是你如何将js数组转换为JSON obj
你可以使用J SON Serialize方法传递j子串...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.