简体   繁体   中英

How to convert JSON to simple 2D Array ?

I have a JSONObject that looks like this

[{"key1":1,"key2":"yyy","key3":"4"..........}, 
{"key1":2,"key2":"xxx","key3":"5"...........}, 
{"key1":3,"key2":"zzz","key3":"6"...........}] 

I need to convert it into a simple 2D array and remove all the keys. Keep values only

[{1,"yyy","4",..........}, 
{2,"xxx","5",...........}, 
{3,"zzz","6",...........}]

I tried iteration and loop bu this seems to be too much hassle. Is there any drop_keys function that will do it ?

    import java.util.Arrays;
    import org.json.JSONArray;

    public class JsonToArray {
       public static void main(String args[]) throws Exception {
          String [] myArray = [{"key1":1,"key2":"yyy","key3":"4"},{"key1":2,"key2":"xxx","key3":"5"},{"key1":3,"key2":"zzz","key3":"6"}];
          JSONArray jsArray = new JSONArray();
          for (int i = 0; i < myArray.length; i++) {
             jsArray.put(myArray[i]);
         }
         System.out.println(jsArray);
         String[] array = new String[myArray.length];
         for (int i = 0; i < myArray.length; i++) {
            array[i] = (String)jsArray.get(i);
         }
         System.out.println("Contents of the array :: "+Arrays.toString(array));
       }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM