2

I'm working on a way to parse JSON files and collect their contents for use elsewhere. I currently have a working example that is as follows:

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class testJSONParser {
    public static void main(String[] args) throws Exception {
        List<Map<String, String>> jsonArray = new ArrayList<Map<String, String>>();

        BufferedReader br = new BufferedReader(new FileReader("json.txt"));

        try {
            String line = br.readLine();

            while (line != null) {
                JSONObject jsonObject = (JSONObject)new JSONParser().parse(line);

                Map<String, String> currentLineMap = new HashMap<String, String>();

                currentLineMap.put("country", jsonObject.get("country").toString());
                currentLineMap.put("size", jsonObject.get("size").toString());
                currentLineMap.put("capital", jsonObject.get("capital").toString());

                jsonArray.add(currentLineMap);

                line = br.readLine();
            }
            } catch (FileNotFoundException fnfe) {
                fnfe.printStackTrace();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                br.close();
            };
        }
    }
}

I'm using the json simple library for parsing the passed in JSON strings.

Here's a sample string from the parsed file.

{"**country**":"Canada","**size**":"9,564,380","**capital**":"Ottawa"}

What my question is about is how to take this code, and have the put method be able to assign to the corresponding Map dynamically. This is what I currently have:

for (int i = 0; i < jsonObject.size(); i++) {
     currentLineMap.put(jsonObject.???.toString(), jsonObject.get(i).toString());
}

The ??? part is where I'm stumped. Getting the values of the current JSON line is easy enough. But how to get the property values (highlighted in bold in the JSON string sample) eludes me. Is there a method that I can call on this object that I'm not familiar with? A different and better way to itenerate through this? Or am I doing this completely assbackwards right from the get go?

0

1 Answer 1

5

In the JSON.org reference implementation, you could do:

for (String key : JSONObject.getNames(jsonObject))
{
    map.put(key, jsonObject.get(key));
}

In JSON simple, you would do:

for (Object keyObject : jsonObject.keySet())
{
    String key = (String)keyObject;
    map.put(key, (String)jsonObject.get(key));
}

This should do the trick.

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

6 Comments

Where is getNames coming from?
As you can see in the code I posted, it is a static method of the JSONObject class.
Are you using the same library? I'm getting "The method getNames(JSONObject) is undefined for the type JSONObject" when I use this code for my project.
My bad, I thought that would refer to the "simple" reference implementation of json.org. Allow me to edit my answer.
No worries perhaps I'm using the wrong name for that library, I'll review and update if needed.
|

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.