2

Im working on a android app and Im having trouble with a Json String. I want to access "Naam" under "Docenten" in the following Json:

{  
"Items":[  
  {  
     "Id":2157750,
     "Links":[  
        {  
           "Rel":"Self",
           "Href":"/api/personen/11824/afspraken/2157750"
        },
        {  
           "Rel":"Next",
           "Href":"/api/personen/11824/afspraken/2157661"
        }
     ],
     "Start":"2015-11-16T07:30:00.0000000Z",
     "Einde":"2015-11-16T08:20:00.0000000Z",
     "LesuurVan":1,
     "LesuurTotMet":1,
     "DuurtHeleDag":false,
     "Omschrijving":"du - rec - G3A",
     "Lokatie":"103",
     "Status":7,
     "Type":13,
     "WeergaveType":0,
     "Inhoud":"<html><head></head><body style=\"font-family: 'Arial'; font-size: 12px\"><p style=\"margin: 0px 0px 10px\">lernen für Klassenarbeit,<br/>fertigmachen: 17.4</p></body></html>",
     "InfoType":1,
     "Aantekening":null,
     "Afgerond":false,
     "Vakken":[  
        {  
           "Id":3,
           "Naam":"Duits"
        }
     ],
     "Docenten":[  
        {  
           "Id":706,
           "Naam":"C. Reuten",
           "Docentcode":"rec"
        }
     ],
     "Lokalen":[  
        {  
           "Naam":"103"
        }
     ],
     "Groepen":null,
     "OpdrachtId":0,
     "HeeftBijlagen":false,
     "Bijlagen":null
  },
  ...

This is my code but it gives a NullPointer Error:

private void handlejson_agenda(String input){
    if(input != null && input != "[]" && input != "") {
        try {
            JSONObject jsonRootObject = new JSONObject(input);

            //Get the instance of JSONArray that contains JSONObjects
            JSONArray jsonArray = jsonRootObject.optJSONArray("Items");

            //Iterate the jsonArray and print the info of JSONObjects
            if(jsonArray == null){
                //niks
            } else {
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);

                    String name = jsonObject.optString("Omschrijving").toString();
                    String lesuurvan = jsonObject.optString("LesuurVan").toString();
                    String lesuurtot = jsonObject.optString("LesuurTotMet").toString();
                    String lokatie = jsonObject.optString("Lokatie").toString();
                    String status = jsonObject.optString("Status").toString();
                    String datumvan = jsonObject.optString("Start").toString();
                    String datumtot = jsonObject.optString("Einde").toString();
                    String leraar = jsonObject.optJSONArray("Docenten").optString("Naam").toString();
                    String inhoudles = jsonObject.optString("Inhoud").toString();
                    String datumvanconverted = datumvan.substring(0, 10);
                    DateFormat dateFormat = new SimpleDateFormat("HH:mm");
                    ...

Everything works except for the "Leraar" String, I hope someone can help me.

-Z3r0byte

2

2 Answers 2

3

This may work:

String leraar = jsonObject.optJSONArray("Docenten").getJSONObject(0).optString("Naam").toString();

So Docenten is a JSONArray that it may need to be accessed with index.

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

Comments

0

I would recommand you to create an Item class with the corresponding fields and then to use ObjectMapper to parse json to java Object.

private ObjectMapper mapper = new ObjectMapper();
Item item = mapper.readValue(input, new TypeReference<Item>(){});
item.getDocenten().get(0).getNaam();

And the Item.class

public class Item {
    // attributes
    // constructor
    // getter + setter
}

It will be much easier after that

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.