Daron
Daron

Reputation: 329

Looking for a Straightforward Way to Parse JSON

I am attempting to parse the following JSON using Java:

{ "student_id": "123456789", "student_name": "Bart Simpson", "student_absences": 1}

What is the simplest way to accomplish this. I tried doing it the way below but think there must be an easier way.

 import org.json.*
 JSONObject obj = new JSONArray("report");

 for(int i = 0; I < arr.length(); i++){
     String studentname =     
         arr.getJSONObject(i).getString("student_id");
         arr.getJSONObject(i).getString("student_name");
         arr.getJSONObject(i).getString("student_name");
 }

Upvotes: 0

Views: 73

Answers (2)

Jemolah
Jemolah

Reputation: 2192

The answer to your question depends on what you want to achieve. Your example would result in an array of strings. The previous answer results in a Java class with a property for each field. Another option is to put the values into a map.

If have written an encoder / decoder combination for this. Encoding is quite easy: use the keys and values of the map. The decoder (JSON string to map or anything else) requires a parser (best would be a tokenizer).

Upvotes: 0

Bart Kiers
Bart Kiers

Reputation: 170178

There's Gson:

import com.google.gson.Gson;
import com.google.gson.annotations.SerializedName;

public class Main {
  public static void main(String[] args) {
    String json = "{ \"student_id\": \"123456789\", \"student_name\": \"Bart Simpson\", \"student_absences\": 1}";
    Student student = new Gson().fromJson(json, Student.class);
    System.out.println(student);
  }
}

class Student {

  @SerializedName("student_id")
  String studentId;

  @SerializedName("student_name")
  String studentName;

  @SerializedName("student_absences")
  Integer studentAbsences;

  @Override
  public String toString() {
    return "Student{" +
      "studentId='" + studentId + '\'' +
      ", studentName='" + studentName + '\'' +
      ", studentAbsences=" + studentAbsences +
      '}';
  }
}

Another popular one is Jackson:

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;

public class Main {
  public static void main(String[] args) throws Exception {
    String json = "{ \"student_id\": \"123456789\", \"student_name\": \"Bart Simpson\", \"student_absences\": 1}";
    Student student = new ObjectMapper().readValue(json, Student.class);
    System.out.println(student);
  }
}

class Student {

  @JsonProperty("student_id")
  String studentId;

  @JsonProperty("student_name")
  String studentName;

  @JsonProperty("student_absences")
  Integer studentAbsences;

  @Override
  public String toString() {
    return "Student{" +
      "studentId='" + studentId + '\'' +
      ", studentName='" + studentName + '\'' +
      ", studentAbsences=" + studentAbsences +
      '}';
  }
}

In both cases, running Main will print:

Student{studentId='123456789', studentName='Bart Simpson', studentAbsences=1}

EDIT

And without creating a Student class, you could give something like JsonPath a try.

Upvotes: 2

Related Questions