我们可以在下面的示例中将 JSONArray 转换为字符串数组
示例
import org.json.*;
import java.util.*;
public class JsonArraytoStringArrayTest {
public static void main(String[] args) {
JSONArray jsonArray = new JSONArray();
jsonArray.put("INDIA ");
jsonArray.put("AUSTRALIA ");
jsonArray.put("SOUTH AFRICA ");
jsonArray.put("ENGLAND ");
jsonArray.put("NEWZEALAND ");
List list = new ArrayList();
for(int i=0; i < jsonArray.length(); i++) {
list.add(jsonArray.getString(i));
}
System.out.print("JSONArray: " + jsonArray);
System.out.print("n");
String[] stringArray = list.toArray(new String[list.size()]);
System.out.print("String Array: ");
for(String str : stringArray) {
System.out.print(str);
}
}
}
登录后复制
输出
JSONArray: ["INDIA ","AUSTRALIA ","SOUTH AFRICA ","ENGLAND ","NEWZEALAND "]
String Array: INDIA AUSTRALIA SOUTH AFRICA ENGLAND NEWZEALAND
登录后复制
以上就是在Java中,我们如何将JSONArray转换为String数组?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!