0

Im making an app thats dynamic to a json template (the app reads the template from the internet) and i need to read a JSONArray's key string.

my json:

{
  "format": {
    "Drive": [
      "Mecanum",
      "Omni",
      "Regular"
    ],
    "Drive-Configuration": [
      "H",
      "X"
    ],
    "Glyph-Elevator": [
      "Parallel Elevator",
      "Stick Elevator",
      "Strip Elevator"
    ],
    "Glyph-Picker": [
      "Strip Picker",
      "Dual-Servo Picker"
    ],
    "Relic-Elevator": [
      "Horizontal Parallel"
    ],
    "Relic-Holder": [
      "Pliers",
      "Dual-Servo With Rubber Pads"
    ],
    "CypherBox-Fill": [
      "0",
      "1",
      "2",
      "3",
      "4",
      "5",
      "6",
      "7",
      "8",
      "9",
      "10",
      "11",
      "12"
    ],
    "Autonomous": [
      "Poor",
      "Minimal",
      "Okay",
      "Good",
      "Almost Perfect",
      "Perfect"
    ]
  }
}

what i want is to read the "Drive" and the "Drive-Configuration" names by code.

what i have:

JSONObject reader=new JSONObject(template);
        JSONArray config=reader.getJSONArray("format");
        for(int type=0;type<config.length();type++){
            JSONArray con=config.getJSONArray(type);
            //Here I Want To Read The Array's Name
        }
1

2 Answers 2

0

Instead of JSON Parser use GSON.!

add this to your gradle

compile 'com.google.code.gson:gson:2.8.1'

Create a ClassMain.!

class MainClass{

@SerializedName("format") 
Value format;



}

Class value{

@SerializedName("Drive") 
ArrayList<String> drive;


@SerializedName("Drive-Configuration") 
ArrayList<String> driveConfiguration;


generate getter and setter.!


}




   and then Convert your JSON to GSON.! 

    MainClass mainClass= new Gson().fromJson(json.toString(), MainClass.class);


    mainClass.getFormat().getDirve();
Sign up to request clarification or add additional context in comments.

Comments

0

Used JSONObject.names(); to read the names

ArrayList<Template> tm = new ArrayList<>();
JSONObject reader = new JSONObject(format);
JSONObject config = reader.getJSONObject("format");
Iterator<String> types = config.keys();
while (types.hasNext()) {
String name = types.next();
tm.add(new Template(name, config.getJSONArray(name)));
}

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.