提取比较所有数组元素的特定json数组元素

时间:2018-01-30 10:45:03

标签: java android json

我有以下格式的json对象。

[
    {
        "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"
            }
        ]
    }
]

在此我需要列出学生ID以及Android自定义列表视图中的最新测试标记

要提取每个学生ID,我正在使用

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");
}

但是我想要学生ID并标记最新测试的详细信息。我想从'StudentMarks'获取'TestId'最大的数组元素。怎么做?

5 个答案:

答案 0 :(得分:1)

我使用以下代码解决了该问题。谢谢@Ravi,我的答案得到了帮助。

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);
}

答案 1 :(得分:0)

请使用以下代码访问studentId和测试结果

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) {}

答案 2 :(得分:0)

找到studentId或Mark非常简单但是为了找到最新的,你应该每次都比较testId并查看最新的测试标记。

要找到studentId使用@Aman Kumar Aggarwal代码并在下面找到最新标记代码将有所帮助

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

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

对象latestMarks将是您的要求

答案 3 :(得分:0)

假设您能够迭代并使用下面的代码行来获取所有StudentMarks

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

然后,IMO,您可以在获得最大TestId时存储值并获得最大值TestId,您可以使用最小值初始化它,并且每当您获得更大的值时,将所有值覆盖为最新值之一。

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 );

答案 4 :(得分:0)

我建议使用 Gson 库,而不是进行手动解析,这会将您的 Json 响应映射到Pojo,然后您可以对 StudentMarksDetail进行排序使用 TestID 之后,您不必迭代查找最新测试的整个列表。您可以轻松拾取已排序列表的最后一个元素。 例如:

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 }

现在,您的StudentMarkDetail基于 TestID 进行排序。现在,您可以轻松地从列表的最后位置拾取最新的TestID

Gson的用法:

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

创建Gson引用后,您可以使用Gson类的内部方法将Json映射到Pojo

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