猿问

如何在 json 对象中创建数组

我正在使用这样的东西 -


String Number1=to_1;

        String Number2=to_2;

        String[] arrayNumbers = new String[] {(char)34+Number1+(char)34,(char)34+Number2+(char)34};

        System.out.println(Arrays.toString(arrayNumbers));

        JSONObject jsonObj = new JSONObject();

        jsonObj.put("to",Arrays.toString(arrayNumbers));

        jsonObj.put("type",type);

        jsonObj.put("callback",callbackUrl);

        JSONArray array = new JSONArray();

        JSONObject Array_item = new JSONObject();

        jsonObj.put(type, array);

        Array_item.put("caption",captionName);

        array.add(Array_item);


        System.out.println(jsonObj.toString());

预期-


{

  "to":["91890xx", "91890xx"],

 "type": "document", "document" : {"caption" : "doc"},

"callback":"{{callback}}"

}

实际-{"document":[{"caption":"hello"}],"callback":"{{callback}}","to":"[\"91890xxx\", \"91890xx\"]","type":"document"}


我不知道任何更多的逻辑来删除到数字的双引号,因为它考虑到一个字符串,其中两个数字都应该是数组格式,如预期中所述。


幕布斯7119047
浏览 440回答 4
4回答

牧羊人nacy

首先,我向您展示正确的代码:import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;public class Test {&nbsp; /**&nbsp; &nbsp;* <pre>&nbsp; &nbsp;*&nbsp; {&nbsp; &nbsp;*&nbsp; &nbsp; "to":["91890xx", "91890xx"],&nbsp; &nbsp;*&nbsp; &nbsp; "type": "document",&nbsp; &nbsp;*&nbsp; &nbsp; "document" : {"caption" : "doc"},&nbsp; &nbsp;*&nbsp; &nbsp; "callback":"{{callback}}"&nbsp; &nbsp;*&nbsp; }&nbsp; &nbsp;* </pre>&nbsp; &nbsp;*&nbsp; &nbsp;* @param args&nbsp; &nbsp;* @throws JSONException&nbsp; &nbsp;*/&nbsp; &nbsp;public static void main(String[] args) throws JSONException {&nbsp; &nbsp; String number1 = "91890";&nbsp; &nbsp; String number2 = "91890";&nbsp; &nbsp; String[] numbers = new String[]{number1, number2};&nbsp; &nbsp; JSONArray toNode = new JSONArray();&nbsp; &nbsp; for (String number : numbers) {&nbsp; &nbsp; &nbsp; toNode.put(number);&nbsp; &nbsp; }&nbsp; &nbsp; JSONObject jsonObj = new JSONObject();&nbsp; &nbsp; jsonObj.put("to", toNode);&nbsp; &nbsp; jsonObj.put("type", "document");&nbsp; &nbsp; jsonObj.put("document", new JSONObject().put("caption", "doc"));&nbsp; &nbsp; jsonObj.put("callback", "{{callback}}");&nbsp; &nbsp; System.out.println(jsonObj.toString());&nbsp; }}结果:{"document":{"caption":"doc"},"callback":"{{callback}}","to":["91890","91890"],"type":"document"}如果要创建一个 josn 数组节点,请显示 use 和 use 方法来添加元素。JSONArrayJSONArray#put(*)将字符串放入 或 中,不需要用引号(“) 包装字符串。此外,你应该写,而不是在Java中有点晦涩难懂。JSONArrayJSONObject\"(char) 34以下情况用于回复注释。import org.json.JSONArray;import org.json.JSONException;import org.json.JSONObject;import java.math.BigDecimal;import java.net.URI;import java.net.URL;import java.util.*;public class Test1 {&nbsp; public static void main(String[] args) throws JSONException {&nbsp; &nbsp; Map<String, Object> map = new HashMap<>();&nbsp; &nbsp; map.put("key1", "value1");&nbsp; &nbsp; map.put("key2", new Date());&nbsp; &nbsp; map.put("key3", 1);&nbsp; &nbsp; map.put("key4", null);&nbsp; &nbsp; map.put("key5", Collections.singletonMap("key5-key1", "value"));&nbsp; &nbsp; map.put("key6", Arrays.asList(1, 2, 3, 4));&nbsp; &nbsp; map.put("key7", BigDecimal.TEN);&nbsp; &nbsp; map.put("key8", new String[]{"a", "b", "c"});&nbsp; &nbsp; map.put("key9", TestEnum.A);&nbsp; &nbsp; map.put("key10", new TestEnum[]{TestEnum.A, TestEnum.B, TestEnum.C});&nbsp; &nbsp; Object json = buildJsonObj(map);&nbsp; &nbsp; System.out.println(json);&nbsp; }&nbsp; private static Object buildJsonObj(Object source) throws JSONException {&nbsp; &nbsp; if (source == null) {&nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; }&nbsp; &nbsp; if (isSimpleValueType(source.getClass())) {&nbsp; &nbsp; &nbsp; return source;&nbsp; &nbsp; }&nbsp; &nbsp; if (source instanceof Map) {&nbsp; &nbsp; &nbsp; Map<Object, Object> map = (Map<Object, Object>) source;&nbsp; &nbsp; &nbsp; JSONObject jsonObject = new JSONObject();&nbsp; &nbsp; &nbsp; for (Map.Entry<Object, Object> entry : map.entrySet()) {&nbsp; &nbsp; &nbsp; &nbsp; Object key = entry.getKey();&nbsp; &nbsp; &nbsp; &nbsp; if (!(key instanceof String)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new IllegalArgumentException("key must be string.");&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; jsonObject.put((String) key, buildJsonObj(entry.getValue()));&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; return jsonObject;&nbsp; &nbsp; }&nbsp; &nbsp; if (source instanceof Iterable) {&nbsp; &nbsp; &nbsp; Iterable<Object> iterable = (Iterable<Object>) source;&nbsp; &nbsp; &nbsp; JSONArray jsonArray = new JSONArray();&nbsp; &nbsp; &nbsp; for (Object value : iterable) {&nbsp; &nbsp; &nbsp; &nbsp; jsonArray.put(buildJsonObj(value));&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; return jsonArray;&nbsp; &nbsp; }&nbsp; &nbsp; if (source.getClass().isArray()) {&nbsp; &nbsp; &nbsp; Object[] array = (Object[]) source;&nbsp; &nbsp; &nbsp; JSONArray jsonArray = new JSONArray();&nbsp; &nbsp; &nbsp; for (Object value : array) {&nbsp; &nbsp; &nbsp; &nbsp; jsonArray.put(buildJsonObj(value));&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; return jsonArray;&nbsp; &nbsp; }&nbsp; &nbsp; throw new IllegalArgumentException("Unsupported type: " + source + ".");&nbsp; }&nbsp; private static boolean isSimpleValueType(Class<?> clazz) {&nbsp; &nbsp; return (Enum.class.isAssignableFrom(clazz) ||&nbsp; &nbsp; &nbsp; &nbsp; CharSequence.class.isAssignableFrom(clazz) ||&nbsp; &nbsp; &nbsp; &nbsp; Number.class.isAssignableFrom(clazz) ||&nbsp; &nbsp; &nbsp; &nbsp; Date.class.isAssignableFrom(clazz) ||&nbsp; &nbsp; &nbsp; &nbsp; URI.class == clazz || URL.class == clazz ||&nbsp; &nbsp; &nbsp; &nbsp; Locale.class == clazz);&nbsp; }&nbsp; public enum TestEnum {&nbsp; &nbsp; A, B, C&nbsp; }}结果:{"key1":"value1","key2":"Thu Mar 14 20:20:49 CST 2019","key5":{"key5-key1":"value"},"key6":[1,2,3,4],"key3":1,"key9":"A","key7":10,"key8":["a","b","c"],"key10":["A","B","C"]}

精慕HU

替换行jsonObj.put(“to”,Arrays.toString(arrayNumbers));自jsonObj.put(“to”, Arrays.asList(arrayNumbers));

守着星空守着你

let a = {"document":[{"caption":"hello"}],"callback":"{{callback}}","to":"[\"91890xxx\", \"91890xx\"]","type":"document"};&nbsp; &nbsp; a = JSON.parse(JSON.stringify(a));&nbsp; &nbsp; let b = JSON.parse(a.to);&nbsp; &nbsp; a.to = b;&nbsp; &nbsp; console.log(a);

紫衣仙女

取代jsonObj.put("to",Arrays.toString(arrayNumbers));跟jsonObj.put("to",arrayNumbers);
随时随地看视频慕课网APP

相关分类

Java
我要回答