I have a JSON file with no clue on how data will be in it nor the structure of data. The only thing known is that it will have either an array of JSON objects or a single JSON object.
I need to get each object from the file and store it as a separate item. In case of array of objects in the file, I should get an array of JSON strings which I can store in DB. Basically, I need to read this file and separate out each JSON object from it and store it in DB as a string.
One of the ways to do it was to use JACKSON ObjectMapper and assign these items to a Hashmap as key value pairs, but I am not sure though how it can be done If there are list of JSON Objects in the file.
Sample JSON File:
[
{
"name":"Bob",
"type":"Email",
"from":"[email protected]",
"to":"[email protected]",
"attachments":[...],
.
.
.
}
]
-
Do you know the Object structure that the JSON has(let it be Array or a single one) ?prasad vsv– prasad vsv2015年03月10日 15:13:08 +00:00Commented Mar 10, 2015 at 15:13
-
so in short u don't know if it starts with '[' or '{' am I right. or could be the file may contains many seperated Json values?nafas– nafas2015年03月10日 15:13:18 +00:00Commented Mar 10, 2015 at 15:13
-
I have updated my question. @nafas Yes, It might contain just one object or array of objects.Abhishek– Abhishek2015年03月10日 15:17:26 +00:00Commented Mar 10, 2015 at 15:17
-
@prasadvsv : JSON Object Structure might not be consistent, It will change for each type of file, I have added a sample JSONAbhishek– Abhishek2015年03月10日 15:18:17 +00:00Commented Mar 10, 2015 at 15:18
-
If you do not care about classes: use a Map<String, Object>. If you care about classes: make a schema parser that output java files.Jazzwave06– Jazzwave062015年03月10日 15:21:59 +00:00Commented Mar 10, 2015 at 15:21
3 Answers 3
Do you know the Object structure that the JSON has(let it be Array or a single one) ? If Yes,
First load the json string form the file into an in memory string.
- check the string for Array existence, by searching for '[',']' in the outer structure of multiple occurrences of '{' or '}'
- once you know whether you have an array or a single object, you can pass it as object reference to either Jackson or GSON parsers
- create in memory Array of JsonObject.class say List. It is actually better to enclose this List inside another class. say myJsonObjects and have a List inside it.
Let us see GSON parsers (by google), though Jackson can also be used in the similar implementation
Gson gson = new Gson();
if(isArray){
myJsonObjects jsonArray = gson.fromJson(jsonStringFromFile,myJsonObjects );
}
else{
gson.fromJson(jsonStringFromFile,JsonObject);
}
http://google-gson.googlecode.com/svn-history/trunk/gson/docs/javadocs/com/google/gson/Gson.html
1 Comment
Jackson is my favorite JSON-to-POJO library. It doesn't really matter where you're loading the JSON from (a URL or from the filesystem), there are handlers for several input sources.
Here's an example:
Map<String,Object> userData = mapper.readValue(new File("user.json"), Map.class);
As far as having an unknown number of JSON structures that you're about to parse, the first thing that comes to mind is to have a mapper for each type you're expecting. You could then wrap the parsing code in try/catch blocks so that if the first fails with whatever exception Jackson gives you when encountering an unexpected format, you can then try the next format and so on.
If you're just trying to generically parse JSON that you don't know the structure of beforehand, you can try something like this:
mapper.readValue(jsonString, new TypeReference<List<EntryType>>() {});
The documentation for Jackson is pretty good-- giving it a solid read-through should definitely help. Here's a good five minute tutorial: http://wiki.fasterxml.com/JacksonInFiveMinutes
7 Comments
I prefer use Gson:
Gson gson;
Map<String, Object>parameters=gson.fromJson(myString);
the rest is iterate the map, i hope help you