How to convert a Array (JSON Array) to Map?

后端 未结 1 913
暖寄归人
暖寄归人 2021-01-29 14:12

My script returns me a Array (JSON Array) as follows :

[{redirectCount=0, encodedBodySize=60962, unloadEventEnd=0, responseEnd=1601.699999999255, domainLookupEnd         


        
相关标签:
1条回答
  • 2021-01-29 14:42

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

    first remove { and [

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

    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);
        }
    }
    

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

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

    0 讨论(0)
提交回复
热议问题