0

I have json and i want to store them 2D array.

This is my json

[  
   {  
      "IsStudent":true,
      "Name":"Ali",
      "age":16,
      "ID":1
   },
   {  
      "IsStudent":false,
      "Name":"Emad",
      "age":17,
      "ID":2
   }
]

so I want to store all the information to 2D array :

array[0][0] = true

array[0][1] = Ali

array[0][2] = 16

array[0][3] = 1

and so on..

I tried alot to get those values by using split and join but it doesn't work for me

JSONArray jarr2 = new JSONArray("my json is here");
String[] resultingArray = jarr.join("\":").split(",\"");
System.out.println(resultingArray[3]);
4
  • stackoverflow.com/questions/12793931/… Commented Jul 21, 2015 at 10:16
  • Arrays have one type and you are adding boolean, string, integers in same array. Make a class, create variables of same type as in json and parse your json by using this class as model. or just treat each value as string i.e. "true","Ali","16","1". Commented Jul 21, 2015 at 10:16
  • no problem i want to store them as string. Commented Jul 21, 2015 at 10:17
  • Don't do that. Create a class having that parameters as fields. Commented Jul 21, 2015 at 10:18

2 Answers 2

0

Read json file example:

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Map;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;


public class Test {

    public static void main(String[] args) {
        FileReader reader;
        try {
            reader = new FileReader("/test.json");
            JsonParser jsonParser =  new JsonParser();
            JsonArray array = (JsonArray) jsonParser.parse(reader);
            searchJsonElemnet(array);
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        }
    }

    private static void searchJsonElemnet(JsonArray jsonArray){
        String[][] matrix = new String[2][4];
        int i =0;
        int j = 0;
        for (JsonElement jsonElement : jsonArray) {
             for (Map.Entry<String, JsonElement> entry : jsonElement.getAsJsonObject().entrySet()) {
                 matrix[i][j] = entry.getValue().toString();
                 j++;
            }
            i++;
            j = 0;
        }
        for (String[] row : matrix)
        {
            for (String value : row)
            {
                System.out.println(value);
            }
        }
    }

}

Output :

true "Ali" 16 1 false "Emad" 17 2

Sign up to request clarification or add additional context in comments.

Comments

0

You can use the library org.json and do something like this:

String jsonData = "[{\"IsStudent\":true,\"Name\":\"Ali\",\"age\":16,\"ID\":1},{\"IsStudent\":false,\"Name\":\"Emad\",\"age\":17,\"ID\":2}]";

JSONArray jsonArray = new JSONArray(jsonData);

for(int i = 0; i < jsonArray.length; i++)
{
    JSONObject jsonObject = jsonArray.getJSONObject(i);

    boolean isStudent = jsonObject.getBoolean("isStudent");
    String name = jsonObject.getString("Name");
    int age = jsonObject.getInt("age");
    int id = jsonObject.getInt("ID");

    //You can do the other stuf that you want with the fetched data.
}

Comments

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.