so you have to iterate through them

Optimize crypto dataset operations with database knowledge and collaboration.
Post Reply
poxoja9630
Posts: 9
Joined: Sun Dec 22, 2024 4:42 am

so you have to iterate through them

Post by poxoja9630 »

In reality, it's 19 . But I'm not panicking (yet). To calculate this from the root JsonNode, we need to: Iterate all NEOs - there is a list of these items for each date, so we'll need a nested loop Increment a philippines mobile number example counter if the field is_potentially_hazardous_asteroid is true.

Here is the code: Java Copy the code int getPotentiallyHazardousAsteroidCount(JsonNode neoJsonNode) { int potentiallyHazardousAsteroidCount = 0; JsonNode nearEarthObjects = neoJsonNode.path("near_earth_objects"); for (JsonNode neoClosestApproachDate : nearEarthObjects) { for (JsonNode neo : neoClosestApproachDate) { if (neo.get("is_potentially_hazardous_asteroid").asBoolean()) { potentiallyHazardousAsteroidCount += 1; } } } return potentiallyHazardousAsteroidCount; } [ this code in the example directory ] This is a bit special: Jackson doesn't allow us to use the Streams API directly , so I resort to a nested loop.

Image

asBoolean returns the value of boolean fields in JSON, but can also be called on other types: numeric nodes will be resolved as true if they were not equal to zero text nodes are true if the value is "true" What is the name and speed of the fastest NEO? The method of finding and iterating through each of the NEOs is the same as in the previous example, but the speed of each NEO is nested under multiple levels, so you have to iterate through them to choose the value kilometers_per_second.

Json Copy the code "close_approach_data": [ { .


path("near_earth_objects"); for (JsonNode neoClosestApproachDate : nearEarthObjects) { for (JsonNode neo : neoClosestApproachDate) { double speed = neo .get("close_approach_data") .get(0) .get("relative_velocity") .get("kilometers_per_second") .asDouble(); if ( fastestNEO == null || speed > fastestNEO.

speed ){ fastestNEO = new NeoNameAndSpeed(neo.get("name").asText(), speed); } } } return fastestNEO; } [ this code in the example directory ] Even though the speeds are stored as strings in JSON, I could call .

asDouble() ‌- Jackson is smart enough to call Double.parseDouble for me. JSON Data Binding to Custom Classes If you have more complex data queries, or need to create objects from JSON to pass to other code, the tree model is not suitable. Jackson offers another mode of operation called data binding , which allows you to directly decompose JSON into objects of your own design.
Post Reply