0

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 ?

2
  • create an object with those keys as an attribute and map the JSON using some JSON object mapper...that should be a much cleaner way to do such things. Commented Oct 25, 2018 at 18:17
  • possible duplicate of stackoverflow.com/questions/8626813/… Commented Oct 25, 2018 at 18:59

1 Answer 1

2
    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));
       }
    }
Sign up to request clarification or add additional context in comments.

2 Comments

String [] myArray = is not valid. Was that supposed to be a JSON OBject or Java Array. Thank you
answer is incorrect . String [] myArray does not compile. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.