I'm trying to parse JSON my own way and I'm stuck on converting List to Map:
public static void main ( final String[] args ) throws Exception
{
{
String test = new String("[{\"label\": 1, \"value\": 12345}, {\"label\": 2, \"value\": 12}]");
final StringReader stringReader = new StringReader(test);
final Object obj = new JSONReader( stringReader ).parseData();
List list = (List)obj; // [{label=1, value=12345}, {label=2, value=12}] looks good
System.out.println(list);
for (Object o : list) {
System.out.println(o); // {label=1, value=12345} {label=2, value=12} also looks ok
if (!(o instanceof Map)) {
throw new IllegalArgumentException("Root object must be a JSON object.");
}
}
}
The following code doesn't throw exception thus the o is instance of Map. However I can't use the o.get("label") to retrieve the label value. How can I get every value from label and value field?