-2

Below Json format is my input, i need to count the total based on Object

Students:[
        {
        "Student_id": "101"
        "Name": "Jon",
        "Course" : "Engineer"
        },
        {
        "Student_id": "102"
        "Name": "Snow",
        "Course" : "Medical"
        },
        {
        "Student_id": "103"
        "Name": "Walter",
        "Course" : "Chemistry"
        },
        {
        "Student_id": "104"
        "Name": "White",
        "Course" : "Chemistry"
        }
    ]

When i loop the JsonArray Am not able to count the total?? it will show always count 1,i want output like below format

No Of Students: 4
Total Chemistry Students :2

Below code i tried,

            JsonArray Students=Details.getAsJsonArray("Students");

            for(int i=0;i<Students.size();i++)
            {
                JsonObject Std=Students.get(i).getAsJsonObject();
                String SrdID=Std.get("Student_id").getAsString();

                System.out.println("Student Details----"+SrdID);
            }


            String [] Studentarr=new String[] {"Student_id"};
            int Count=Studentarr.length;

            System.out.println("Count ---"+ Count);

Any Help will be Appreciate!! Thanks In Advance

4
  • 3
    What have you tried so far? Commented Mar 14, 2019 at 16:53
  • When i loop the JsonArray Am not able to count the total?? it will show always count 1 -- Share the code that apparently does this. Also, which json library are you using? Commented Mar 14, 2019 at 16:54
  • @NicholasK Gson Commented Mar 14, 2019 at 16:55
  • show the code whatever you tried? Commented Mar 14, 2019 at 16:59

2 Answers 2

0

Given that you know the structure for each element in the array is the same, could you create a Student class and have an array of Student objects?

In my experience, it's always better to manipulate objects than pure json, but you've not said in your question if that is a requirement or not, so I'll assume it is.

Therefore, you may find the answer on this similar question useful: how to count length of the JSON array element

Hope this helps!

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

Comments

0

With this structure:

var object = {"Students":[
    {
    "Student_id": "101",
    "Name": "Jon",
    "Course" : "Engineer"
    },
    {
    "Student_id": "102",
    "Name": "Snow",
    "Course" : "Medical"
    },
    {
    "Student_id": "103",
    "Name": "Walter",
    "Course" : "Chemistry"
    },
    {
    "Student_id": "104",
    "Name": "White",
    "Course" : "Chemistry"
    }]};

Now, we can try with JS this:

print(object.Students.length) // print total
print(object.Students.filter(st => st.Course === 'Chemistry').length) // print occurrences

Output is:

> 4
> 2

1 Comment

in java we cant pass the pass st => st.Course === 'Chemistry').length in stream like this coz its not accepted 'Chemistry' it will accept single Char 'e'

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.