1. Engineering
  2. Computer Science
  3. problems about java code level introduction to java write a...

Question: problems about java code level introduction to java write a...

Question details

problems about java code

level: introduction to java

Write a method as shown below:

protected static ArrayList parse(String json)

This method should populate and return an ArrayList with a String of each object from the JSON array structure (stored in the json variable). It is recommended to look at String methods (given here).

You'll need to parse out the JSON data into the ArrayList. Of course, the start of a JSON object (marked by { ) and the end (marked by } ) are very important for us to detect. For example, if the input string is the following, the bold curly brackets indicate the start and end of the objects:

String inputExample = "{"name: " the first json object}, {"name: " the second json object}, {"name: " the third json object}"

// We assume that a json object always has a specific format which is {content}.

Then, the expected return value should be a string type array list.

ArrayList output = parse(inputExample);

===> output = [{"name: " the first json object}, {"name: " the second json object}, {"name: " the third json object}]

System.out.println(output.get(0))

===> {"name: " the first object}

System.out.println(output.get(1))

===> {"name: " the second object}

System.out.println(output.get(2))

===> {"name: " the third object}

Another example:

There is a input string below. The bold curly brackets indicate the start and end of the json objects:

(applicable date 2013-04-30, air pressure: null, created 2013-04-30T20:55:13.980010Z, id 418759, min temp 11.01, max_temp: 22.28, thetemp null, humidity: 61, visibility: 9.997862483098784, predictability: 70, weather_state_name: Light Cloud, weather state abbr: 1С, wind direction: 299, wind direction compass: WNW, wind-speed: 12.35 }, { applicable date2013-04-30 air pressure: 1e12, id 415934, created 2013-04-30T18:55:10.6718207, min_ temp: null, max_temp: null, the temp 27.67, humidity 37, visibility: 18.191263023940188, predictability 68, weather_state_name Clear, weather-state-abbr: с, wind-direction : 315, wind direction-compass: NN, wind speed. 9.2608902 }, (applicable_date 2013-04-30, air pressure null,id: 422086,created: 2013-04-30T16:55:05.437890Z, min_temp: 11.01, max_temp: 22.24, the temp: null, humidity: 66,visibility :9.997862483098784, predictability: 70, weather_state_name: Light Cloud, weather_state_abbrlc, wind direction: 299, wind direction_compass: WNW, wind speed:11.251 compass 2018ty 9.260mptate

Then after calling the parse() function, the returned ArrayList should contain the following three Strings:

Element o: applicable date 2013-04-30 air pressure null, created 2013-04-30T20:55:13.980010Z id 418759, min temp 11.01, max temp 22.28, the temp: null, humidity: 61, visibility: 9.997862483098704, predictability: 7Θ, weather state-name: Light Cloud, weather-state-abbr: lc, wind-direction: 299 , wind direction, compass: WNW, wind_speed 12.35 Element 1: [applicable_date: 2013-04-30 air pressure: 1012, d 415934, created: 2013-04-30T18:55:10.671820Z, min temp null, max temp null, the_temp: 27.67, humidity 37, visibility: 18.191263023940188, predictability 68, weather state-name: Clear, weather-state-abbr: С wind direction: 315, wind direction compass NW wind_speed: 9.2608902 Element 2: applicable_date 2013-04-30, air pressure null,id: 422086, created:2013-04 30T16:55:05.437890Z, min_temp: 11.01,max temp: 22.24,the_temp: null, humidity: 66, visibility:9.997862483098704, predictability: 70, weather_state_name:Light Cloud, weather_state_abbr:Lc, wind_direction:299, wind direction_compass: WNW, wind_speed:11.25

Note each element contains the starting and ending curly brackets. There shouldn't be any format changes (such as spaces, newlines, etc) in each JSON object compared with the parameter string.

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution