2

I have the json object in the following format.

[
    {
        "StudentID": 1,
        "StudentMarks": [
            {
                "StudentID": 1,
                "English": 1500000,
                "Maths": 15,
                "Science": 15,
                "History": 10,
                "TestID": 1,
                "Date": "2018-01-29T14:38:11.01"
            },
            {
                "StudentID": 1,
                "English": 155,
                "Maths": 1578,
                "Science": 15,
                "History": 10,
                "TestID": 2,
                "Date": "2018-01-29T14:38:11.01"
            },
            {
                "StudentID": 1,
                "English": 155,
                "Maths": 15,
                "Science": 150,
                "History": 10,
                "TestID": 3,
                "Date": "2018-01-29T14:38:11.01"
            },
            {
                "StudentID": 1,
                "English": 150,
                "Maths": 15,
                "Science": 15,
                "History": 10,
                "TestID": 6,
                "Date": "2018-01-29T14:38:11.01"
            },
            {
                "StudentID": 1,
                "English": 155,
                "Maths": 1578,
                "Science": 15,
                "History": 10,
                "TestID": 7,
                "Date": "2018-01-29T14:38:11.01"
            }
        ]
    },
    {
        "StudentID": 2,
        "StudentMarks": [
            {
                "StudentID": 2,
                "English": 155,
                "Maths": 151,
                "Science": 15,
                "History": 1025,
                "TestID": 4,
                "Date": "2018-01-29T14:38:11.01"
            },
            {
                "StudentID": 2,
                "English": 1551,
                "Maths": 15,
                "Science": 15,
                "History": 100,
                "TestID": 5,
                "Date": "2018-01-29T14:38:11.01"
            },
            {
                "StudentID": 2,
                "English": 1,
                "Maths": 1,
                "Science": 15,
                "History": 10,
                "TestID": 8,
                "Date": "2018-01-29T14:38:11.01"
            }
        ]
    }
]

In this i need to list the student id and also the latest test marks in an android custom listview,

To extract each student id, i am using

JSONArray myListsAll= new JSONArray(jsonStr);
for(int i=0;i<myListsAll.length();i++)
{
    JSONObject c= (JSONObject) myListsAll.get(i);
    History= c.getString("History");
    Science= c.getString("Science");
    Maths= c.getString("Maths");
    English= c.getString("English");
}

But I want the student id and marks details of the latest test. which i thought of getting array element from 'StudentMarks' for which 'TestId' is maximum. How to do that?

Roohi Zuwairiyah
  • 363
  • 3
  • 15

5 Answers5

1

I resolved the issue using the following code. Thanks @Ravi from whose answer i derived help.

JSONArray myListsAll= new JSONArray(jsonStr);
int test_id = Integer.MIN_VALUE;
for(int i=0;i<myListsAll.length();i++)
{
    JSONObject obj= (JSONObject) myListsAll.get(i);
    id = obj.getString("StudentID");
    JSONArray sm = obj.optJSONArray("StudentMarks");
    for (int j=0; j<sm.length(); j++) 
    {
        JSONObject marksobj= (JSONObject) sm.get(j);
        int curTestId = marksobj.getInt("TestID");
        if (test_id < curTestId) {
            English= marksobj.getString("English");
            Science= marksobj.getString("Science");
            Maths= marksobj.getString("Maths");
            History= marksobj.getString("History");
            test_id = curTestId;
        }
    }
    HashMap<String, String> student= new HashMap<>();
    student.put("id", id);
    student.put("English", English);
    student.put("Science", Science);
    student.put("Maths", Maths);
    student.put("History", History);
    studentslist.add(student);
}
Roohi Zuwairiyah
  • 363
  • 3
  • 15
0

Please use below code to access studentId and test result

try {
    JSONArray myListsAll= new JSONArray(jsonStr);
    for(int i=0;i<myListsAll.length();i++)
    {
        JSONObject c= (JSONObject) myListsAll.get(i);
        int studentId = c.optInt("StudentID", -1);

        int history, science, maths, english;
        JSONArray sm = c.optJSONArray("StudentMarks");
        for (int j=0; j<sm.length(); j++) {
            history= obj.getString("History");
            science= obj.getString("Science");
            maths= obj.getString("Maths");
            english= obj.getString("English");
        }
    }
} catch(Exception e) {}
Aman Kumar Aggarwal
  • 557
  • 1
  • 5
  • 16
0

It is pretty simple to find the studentId or Mark but to find the latest you should compare the testId every time and check the latest test mark.

To find studentId use @Aman Kumar Aggarwal code and to find latest marks below code will help

int previousId=0,currentId=0;
JSONObject latestMarks;

for{
.....
currentId = obj.getString("TestID");
if(currentId > previousId){
     previousId = currentID;
latestMarks = (JSONObject) myListsAll.get(i);
}
}

the Object latestMarks will be your requirement

RajatN
  • 223
  • 1
  • 8
0

Assuming you are able to iterate through and you are using below line of code to get all StudentMarks

JSONArray myListsAll= new JSONArray(jsonStr);
for(int i=0;i<myListsAll.length();i++)
{
    JSONObject obj= (JSONObject) myListsAll.get(i);
    History= obj.getString("History");
    ...
}

Then, IMO, you could store value when you get maximum TestId and to get the maximum TestId, you could initialize it with minimum value and whenever you get greater value overwrite all values to the latest one.

JSONArray myListsAll= new JSONArray(jsonStr);
int test_id = Integer.MIN_VALUE;
for(int i=0;i<myListsAll.length();i++)
{
    JSONObject obj= (JSONObject) myListsAll.get(i);
    int curTestId = obj.getInt("TestID");
    if (test_id < curTestId)
    {
       History= obj.getString("History");
       Science= obj.getString("Science");
       Maths= obj.getString("Maths");
       English= obj.getString("English");
       test_id = curTestId;
    }
}

System.out.print("Maximum Test Id for this student :" + test_id );
Ravi
  • 30,829
  • 42
  • 119
  • 173
0

Instead of doing the manual parsing I would suggest to use the Gson library which will map your Json response to Pojo and then you can sort StudentMarksDetail using the TestID after that you do not have to iterate the entire list of find out then latest test. You can easily pickup the last element of the sorted list. For Example:

data class StudentDetail(val list: MutableList<StudentMarkDetail>? = null, val studentId: Int? = null)

data class StudentMarkDetail(
        val studentID: String,
        val english: Int,
        val maths: Int,
        val science: Int,
        val history: Int,
        val testID: Int,
        val date: String
)

val studentDetail = StudentDetail()
studentDetail?.list?.sortBy { it.testID }

Now your StudentMarkDetail is sorted based on TestID. Now you can easily pickup the latest TestID from the last position for the list

And the usage of Gson:

val gsonBuilder = GsonBuilder()
val gson = gsonBuilder.registerTypeAdapterFactory(
NullStringToEmptyAdapterFactory()).create()

After creating the Gson reference you can use this internal method of Gson class to map the Json to Pojo

public <T> T fromJson(String json, Class<T> classOfT) throws JsonSyntaxException {
    Object object = fromJson(json, (Type) classOfT);
    return Primitives.wrap(classOfT).cast(object);
  }
pk4393
  • 322
  • 3
  • 16