- Engineering
- Computer Science
- 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:
Then after calling the parse() function, the returned ArrayList should contain the following three Strings:
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
