将 JSONArray 转换为 String[] 数组

我有一些问题像这样..

["IMG-20181223-WA0010.jpg","IMG-20181225-WA0013.jpg","IMG-20181229-WA0001.jpg"]

java - 如何从java中的真实数组转换JSONArray?对不起英语不好..


慕容森
浏览 1037回答 4
4回答

FFIVE

我把它放到JSONArray字符串toString()中,我用这样的正则表达式转换..public String getImg(String d){    return rubahFormat(d).split(",");}public String rubahFormat(String d){    return d.replaceAll("[\\[\\]\\\"]","");}谢谢 ..

慕娘9325324

您可以使用 Java Streams 为您做到这一点:    String[] data = new String[] { "value1", "value2", "value3" };    String jsonArray = "[\"" + Stream.of(data).collect(Collectors.joining("\", \"")) + "\"]";蒸汽收集器将", "两个值相加。现在只需["在开头加上相应的结尾。

米琪卡哇伊

你应该这样尝试,它会帮助你import net.sf.json.JSONArray;public class JsonArraytoArray {&nbsp; &nbsp; JSONArray jsonArray = new JSONArray();&nbsp; &nbsp; public void convertJsonarrtoArray() {&nbsp; &nbsp; &nbsp; &nbsp; jsonArray.add("java");&nbsp; &nbsp; &nbsp; &nbsp; jsonArray.add("test");&nbsp; &nbsp; &nbsp; &nbsp; jsonArray.add("work");&nbsp; &nbsp; &nbsp; &nbsp; String[] stringArray = new String[jsonArray.size()];&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < jsonArray.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stringArray[i] = jsonArray.getString(i);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("stringArray&nbsp; " + stringArray.length);&nbsp; &nbsp; }&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; JsonArraytoArray d = new JsonArraytoArray();&nbsp; &nbsp; &nbsp; &nbsp; d.convertJsonarrtoArray();&nbsp; &nbsp; }}

翻阅古今

String[] stringArray = new String[jsonArray.length()];for (int i = 0; i < jsonArray.length(); i++) {&nbsp; &nbsp; stringArray[i]= (String)jsonArray.getJSONObject(i);}jsonArray是您的 JSON 对象和stringArray变量将存储为字符串数组类型
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java