且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将数组(JSON数组)转换为Map?

更新时间:2023-01-17 12:00:55

对于不太复杂的json,您可以尝试手动执行...

for not so complicated jsons, you can try doing it manually...

先删除{和[

jsontext = jsontext.replaceAll ("[\\[\\]{}]", "");

然后使用,分隔符分割json数组

then split the json array using the , separator

String[] items = jsontext.split(",");

然后针对每个项目,使用=分隔符进行拆分,然后填充到您的数组中

then for each item, split using the = separator, and then populate into your array

Map<String, String> array;

for (String s: items){

    String[] item = s.split("=");
    if(item.length == 2){
        array.put(item[0].trim(), item[1].trim());
    }else{
        System.out.println("Error with: "+ s);
    }
}

如果要?作为您的地图数组,那么您必须自己进行从String到Int等的转换...

if you want ? as your map array, then you have to do your own conversion from String to Int, etc...

对于更复杂的json(数组中的数组等),您可能必须先解析并保存内部数组,然后再解析外部数组.

for more complicated jsons (arrays within arrays, etc), you may have to resolve and save the inner arrays first, then resolve the outer arrays.