I am having issues with "converting" the data in the JSON file to the required java objects. The JSON file looks something like this:
{
"initialInventory": [
{"bookTitle": "Harry Poter", "amount": 10, "price": 90},
{"bookTitle": "The Hunger Games", "amount": 90, "price": 102}
],
"initialResources":[
{"vehicles":[
{"license":123483, "speed": 2},
{"license":999994, "speed": 4}
]
}
],
"services":{
"time": {
"speed": 1000,
"duration": 24
},
"selling": 6,
"inventoryService": 3,
"logistics": 4,
"resourcesService": 2,
"customers": [
{
"id": 123456789,
"name": "Bruria",
"address": "NewYork 123",
"distance":33,
"creditCard":{"number":67890,"amount":88},
"orderSchedule": [
{"bookTitle": "Harry Poter", "tick": 3},
{"bookTitle": "The Hunger Games", "tick": 3}
]
},
{
"id": 234567891,
"name": "Shraga",
"address": "BeerSheva 3333",
"distance":12,
"creditCard":{"number":453536,"amount":220},
"orderSchedule": [
{"bookTitle": "The Hunger Games", "tick": 12}
]
}
]
}
All the JSON files I will be dealing with (for now) have the same structure.
I tried using Gson to parse the file (I am required to use Gson for this),
I've searched many videos and tutorials regarding the matter, but most of them were over-simplified or simply irrelevant, and it feels like either there are too many ways to do it, or that I just can't understand how to do it correctly.
This is what I tried so far:
public class BookStoreRunner {
public static void main(String[] args) throws IOException { //has to be added, we get an error without it. can be dealt with try-catch, doesn't matter.
Gson gson=new Gson();
File jsonfile= new File("pathtofile"); //should be the actual path to the file.
InputStream iStream= new FileInputStream(jsonfile);
Reader reader = new InputStreamReader(iStream);
JsonReader jsonReader =gson.newJsonReader(reader);
JsonParser parser= new JsonParser();
BookInventoryInfo[] books=gson.fromJson("initialInventory",BookInventoryInfo[].class);
ResourcesHolder.getInstance().load(gson.fromJson("vehicles",DeliveryVehicle[].class));
int timespeed= gson.fromJson("speed",int.class);
int timeduration=gson.fromJson("duration",int.class);
int numOfSellers=gson.fromJson("selling",int.class);
int numOfInventories=gson.fromJson("inventoryService",int.class);
int numOfLogistics=gson.fromJson("logistics",int.class);
int numOfResourceServices=gson.fromJson("resourcesService",int.class);
Customer[] customers=gson.fromJson("customers",Customer[].class);
/* JsonElement jElement = parser.parse(reader); // JsonElement is equivalent to a list in java
JsonObject jObject = jElement.getAsJsonObject(); //equivalent to an object of a class that implements the 'map' interface, e.g contains pairs of key:value
JsonObject initialInventory = jObject.getAsJsonObject("initialInventory");
JsonArray inventory =initialInventory.getAsJsonArray(); //it is an array (json syntax), but a JSONArray is not iterable.
// dealing with inventory
int i=0; //counter for all the books to be inserted to inventory array (index)
for (Map.Entry<String,JsonElement> entry: initialInventory.entrySet()) //.entrySet() gives a set view of the JSONObject, which is iterable
{
}
JsonArray iResources= jObject.getAsJsonArray("initialResources");
JsonObject veh = iResources.get(0).getAsJsonObject();
//initialization of java object, for the initialResources
JsonObject services=jObject.getAsJsonObject("services");
JsonObject timedata=jObject.getAsJsonObject("services").getAsJsonObject("time");
int numOfSelling= gson.fromJson(jObject.getAsJsonObject("services").getAsJsonPrimitive("selling"), int.class);
int numOfInventories= gson.fromJson(jObject.getAsJsonObject("services").getAsJsonPrimitive("inventoryService"), int.class);
int numOfIogistics=gson.fromJson(jObject.getAsJsonObject("services").getAsJsonPrimitive("logistics"),int.class);
int numOfResourceServices= gson.fromJson(jObject.getAsJsonObject("services").getAsJsonPrimitive("resourcesService"),int.class);
JsonArray jsonCustomers = jObject.getAsJsonObject("services").getAsJsonArray("customers");
JsonObject jsonCustomersObject=jObject.getAsJsonObject("services").getAsJsonObject("customers");
Customer[] customers = new Customer[5000]; // check if there's a problem with this part. unsure how many customers there are, or if we need\can use a list.
int i=0; //customers index
for (Map.Entry<String,JsonElement> entry: jsonCustomersObject.entrySet())
{ //assuming we will implement a constructor for Customer which receives all the required values
}
*/
I've tried parsing the file with a different approach, it's marked as a comment at the end of the code.
I can't figure out if the problem is with my understanding of JSON in general, my knowledge of java I\O usage or that I'm simply missing something.
How can I create the Objects I need from the JSON file effectively with Gson?
Related
I have json file in below format.
{
"data":[
{
"prjId": 1,
"name" : "Forj1",
"issue": [
{
"id": 00001,
"status" : "Closed"
},
{
"id": 00002,
"status" : "Open"
}
]
},
{
"prjId": 2,
"name" : "Forj2",
"issue": [
{
"id": 00003,
"status" : "Closed"
},
{
"id": 00004,
"status" : "Open"
}
]
}],
"issueCounter": 7,
"success": true
}
Here "data" is array of projects, and within project attribute there is array of "issue".
So far if I remove "issue" array, I am able to traverse the json to one level down in "data" attribute, If this json has "issue" array I get an error saying missing comma.
javax.json.stream.JsonParsingException: Invalid token=NUMBER at (line no=15, column no=14, offset=242) Expected tokens are: [COMMA]
Below is the code that I have right now. I have two problems with this, one is the error while reading if I place the "issue" attribute, and secondly a way to read the "issue" array and traverse all attributes within.
InputStream fis = new FileInputStream(pathToFile+"data3.json");
JsonReader jsonReader = Json.createReader(fis);
//the error is thrown on below line while reading the above json.
JsonObject jsonObject = jsonReader.readObject();
jsonReader.close();
fis.close();
System.out.println(jsonObject.getInt("issueCounter"));
//reading arrays from json
JsonArray jsonArrayData = jsonObject.getJsonArray("data");
Project [] prj = new Project[jsonArrayData.size()];
int index = 0;
for(JsonValue value : jsonArrayData){
JSONObject jsonObj = new JSONObject(value.toString());
System.out.println(jsonObj.getString("name"));
System.out.println(jsonObj.getInt("prjId"));
//this is also the place where I am stuck, I know I need to construct an array out of it by obtaining issue attribute. Below is very very wrong.
/*
JsonArray jsonArrayIssue = jsonObj.getJsonArray("issue");
for(JsonValue issue : jsonArrayIssue){
JSONObject jsonIssueObj = new JSONObject(issue.toString());
System.out.println(jsonIssueObj.getString("status"));
System.out.println(jsonIssueObj.getInt("id"));
}
*/
}
Any help or pointers is deeply appreciated. I can tweak the json if its required ultimately I need to maintain an array of issues.
The problem as others said is the JSON.
"id": 00001 <-- this is a number, numbers cannot start with a leading zero as per JSON stadard.
If you control the JSON you should tweak it.
Alternatively ff you don't, you can use a less strict parser like org.json.simple https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple
The code will be the same as yours, just adjusted to org.json.simple
try { ...
JSONObject rootJSON = (JSONObject) new JSONParser().parse(jsonString);
JSONArray dataList = (JSONArray) rootJSON.get("data");
for(Object projectObj: dataList.toArray()){
JSONObject project = (JSONObject)projectObj;
JSONArray issueList = (JSONArray) project.get("issue");
for(Object issueObj: issueList.toArray()){
JSONObject issue = (JSONObject) issueObj;
//do something with the issue
}
}
} catch (ParseException e) {
//do smth
e.printStackTrace();
}
Your json data is invalid.You can check here.
http://jsonlint.com
...issue": [{ "id": 00001,
"status": ----------------------^
Your id must be string number,string,boolean.Send 1,2,3,.... as return values and check if it works.
Your code looks okay the problem is the JSON formatting. Specifically the following lines:
"id": 00001,
"id": 00002,
"id": 00003,
"id": 00004,
Basically if you want it in that format you will need to set them as strings by wrapping the values in quotations i.e. "id": "00001" or you can use a valid number i.e. "id": 1
I'm a new Java programmer coming from a background in Python. I have weather data that's being collected/returned as a JSON with nested keys in it, and I don't understand how pull the values out in this situation. I'm sure this question has been asked before, but I swear I've Googled a great deal and I can't seem to find an answer. Right now I'm using json-simple, but I tried switching to Jackson and still couldn't figure out how to do this. Since Jackson/Gson seem to be the most used libraries, I'd would love to see an example using one of those libraries. Below is a sample of the data, followed by the code I've written so far.
{
"response": {
"features": {
"history": 1
}
},
"history": {
"date": {
"pretty": "April 13, 2010",
"year": "2010",
"mon": "04",
"mday": "13",
"hour": "12",
"min": "00",
"tzname": "America/Los_Angeles"
},
...
}
}
Main function
public class Tester {
public static void main(String args[]) throws MalformedURLException, IOException, ParseException {
WundergroundAPI wu = new WundergroundAPI("*******60fedd095");
JSONObject json = wu.historical("San_Francisco", "CA", "20100413");
System.out.println(json.toString());
System.out.println();
//This only returns 1 level. Further .get() calls throw an exception
System.out.println(json.get("history"));
}
}
The function 'historical' calls another function that returns a JSONObject
public static JSONObject readJsonFromUrl(URL url) throws MalformedURLException, IOException, ParseException {
InputStream inputStream = url.openStream();
try {
JSONParser parser = new JSONParser();
BufferedReader buffReader = new BufferedReader(new InputStreamReader(inputStream, Charset.forName("UTF-8")));
String jsonText = readAll(buffReader);
JSONObject json = (JSONObject) parser.parse(jsonText);
return json;
} finally {
inputStream.close();
}
}
With Jackson's tree model (JsonNode), you have both "literal" accessor methods ('get'), which returns null for missing value, and "safe" accessors ('path'), which allow you to traverse "missing" nodes. So, for example:
JsonNode root = mapper.readTree(inputSource);
int h = root.path("response").path("history").getValueAsInt();
which would return the value at given path, or, if path is missing, 0 (default value)
But more conveniently, you can just use JSON pointer expression:
int h = root.at("/response/history").getValueAsInt();
There are other ways too, and often it is more convenient to actually model your structure as Plain Old Java Object (POJO).
Your content could fit something like:
public class Wrapper {
public Response response;
}
public class Response {
public Map<String,Integer> features; // or maybe Map<String,Object>
public List<HistoryItem> history;
}
public class HistoryItem {
public MyDate date; // or just Map<String,String>
// ... and so forth
}
and if so, you would traverse resulting objects just like any Java objects.
Use Jsonpath
Integer h = JsonPath.parse(json).read("$.response.repository.history", Integer.class);
Check out Jackson's ObjectMapper. You can create a class to model your JSON then use ObjectMapper's readValue method to 'deserialize' your JSON String into an instance of your model class. And vice-versa.
Try jpath API. It's xpath equivalent for JSON Data. You can read data by providing the jpath which will traverse the JSON data and return the requested value.
This Java class is the implementation as well as it has example codes on how to call the APIs.
https://github.com/satyapaul/jpath/blob/master/JSONDataReader.java
Readme -
https://github.com/satyapaul/jpath/blob/master/README.md
Example:
JSON Data:
{
"data": [{
"id": "13652355666_10154605514815667",
"uid": "442637379090660",
"userName": "fanffair",
"userFullName": "fanffair",
"userAction": "recommends",
"pageid": "usatoday",
"fanPageName": "USA TODAY",
"description": "A missing Indonesian man was found inside a massive python on the island of Sulawesi, according to local authorities and news reports. ",
"catid": "NewsAndMedia",
"type": "link",
"name": "Indonesian man swallowed whole by python",
"picture": "https:\/\/external.xx.fbcdn.net\/safe_image.php?d=AQBQf3loH5-XP6hH&w=130&h=130&url=https%3A%2F%2Fwww.gannett-cdn.com%2F-mm-%2F1bb682d12cfc4d1c1423ac6202f4a4e2205298e7%2Fc%3D0-5-1821-1034%26r%3Dx633%26c%3D1200x630%2Flocal%2F-%2Fmedia%2F2017%2F03%2F29%2FUSATODAY%2FUSATODAY%2F636263764866290525-Screen-Shot-2017-03-29-at-9.27.47-AM.jpg&cfs=1&_nc_hash=AQDssV84Gt83dH2A",
"full_picture": "https:\/\/external.xx.fbcdn.net\/safe_image.php?d=AQBQf3loH5-XP6hH&w=130&h=130&url=https%3A%2F%2Fwww.gannett-cdn.com%2F-mm-%2F1bb682d12cfc4d1c1423ac6202f4a4e2205298e7%2Fc%3D0-5-1821-1034%26r%3Dx633%26c%3D1200x630%2Flocal%2F-%2Fmedia%2F2017%2F03%2F29%2FUSATODAY%2FUSATODAY%2F636263764866290525-Screen-Shot-2017-03-29-at-9.27.47-AM.jpg&cfs=1&_nc_hash=AQDssV84Gt83dH2A",
"message": "Akbar Salubiro was reported missing after he failed to return from harvesting palm oil.",
"link": "http:\/\/www.usatoday.com\/story\/news\/nation-now\/2017\/03\/29\/missing-indonesian-man-swallowed-whole-reticulated-python\/99771300\/",
"source": "",
"likes": {
"summary": {
"total_count": "500"
}
},
"comments": {
"summary": {
"total_count": "61"
}
},
"shares": {
"count": "4"
}
}]
}
Code snippet:
String jPath = "/data[Array][1]/likes[Object]/summary[Object]/total_count[String]";
String value = JSONDataReader.getStringValue(jPath, jsonData);
I know its an array, but I am completely new to JSON and need help comprehending how this is structured, here is my attempt at extracting data:
String JSonString = readURL("//my URL is here");
JSONArray s = JSONArray.fromObject(JSonString);
JSONObject Data =(JSONObject)(s.getJSONObject(0));
System.out.println(Data.get("name"));
My JSON data that I have goes like this :
{
"sports": [
{
"name": "basketball",
"id": 40,
"uid": "s:40",
"leagues": [
{
"name": "National Basketball Assoc.",
"abbreviation": "nba",
"id": 46,
"uid": "s:40~l:46",
"groupId": 7,
"shortName": "NBA",
"athletes": []
}
]
}
],
"resultsOffset": 10,
"resultsLimit": 10,
"resultsCount": 1,
"timestamp": "2013-11-18T03:15:43Z",
"status": "success"
}
I dont really have a strong grasp of this stuff so all the help is appreciated.
Here is the idea :
JSONObject root = new JSONObject(yourJsonString);
JSONArray sportsArray = root.getJSONArray("sports");
// now get the first element:
JSONObject firstSport = sportsArray.getJSONObject(0);
// and details of the first element
String name = firstSport.getString("name"); // basketball
int id = firstSport.getInt("id"); // 40
JSONArray leaguesArray = firstSport.getJSONArray("leagues");
// and so on, you can process leaguesArray similarly
It should work (feel free to complain about compile errors if there are any)
Your JSON data is an object (it starts with a curly brace). In the next inner layer, there is a single array (at key "sports"):
String jsonString = readURL("//my URL is here");
JSONObject result = JSONObject(jsonString);
JSONArray sports = result.getJSONArray("sports");
JSONObject sport = sport.getJSONObject(0);
System.out.println(sport.getString("name"));
I might have used another JSON library than you.
JSON means JavaScript Object Notation.
Objects in javascripts are just containers and can be represented by key-value pairs. Please find below notations to understand about json.
Represent objects in json: E.g. Student
{"name" : "Robin", "rollnumber" : "1"}
Represent array in json : E.g. Array of students
[{"name" : "Robin", "rollnumber" : "1"}, {"name" : "Mark", "rollnumber" : "2"}]
You can understand more on JSON from diagrams on this link http://www.json.org/fatfree.html
There are various ways available to to convert JSON to javaobject and javaobject to JSON : One of them is http://wiki.fasterxml.com/JacksonInFiveMinutes
Adding detailed code here along with the imports .
If this helps.
import org.json.JSONException;
import org.json.JSONObject;
public class extractingJSON {
public static void main(String[] args) throws JSONException {
// TODO Auto-generated method stub
String jsonStr = "{\"name\":\"SK\",\"arr\":{\"a\":\"1\",\"b\":\"2\"},\"arrArray\":[{\"a\":\"1\",\"b\":\"2\"}]}";
JSONObject jsonObj = new JSONObject(jsonStr);
String name = jsonObj.getString("name");
System.out.println(name);
String first = jsonObj.getJSONObject("arr").getString("a");
System.out.println(first);
first = jsonObj.getJSONArray("arrArray").getJSONObject(0).getString("a");
System.out.println(first);
}
}
This is my JSON data:
{
"boards": [
{
"board": "3",
"title": "3DCG",
"ws_board": 1,
"per_page": 15,
"pages": 11
},
{
"board": "a",
"title": "Anime & Manga",
"ws_board": 1,
"per_page": 15,
"pages": 11
},
{
"board": "adv",
"title": "Advice",
"ws_board": 1,
"per_page": 15,
"pages": 11
},
...
]
}
This is my code for deserialization:
JSONObject json = readJsonFromUrl("http://api.4chan.org/boards.json");
String jsonBoards = json.toString();
Gson gson = new Gson();
Map<String, Object> map = new HashMap<String,Object>();
map = (Map<String,Object>) gson.fromJson(jsonBoards, map.getClass());
But it doesn't get the job done. I need a way to get the boards name, title, and all the that information for each board. When I use map.get(); the key is "boards" but I want a map of every board and title and so on.
You need a class structure that maps your JSON data. You don't need to use a Map anywhere, since your JSON does not represent any map!
In fact it represents an object {...} that contains a field called "boards", which in turn represents an array [...] of objects {...}...
So the class structure that matches your JSON would be something like this (in pseudo-code):
class Response
List<Board> boards
class Board
String board
String title
int ws_board
int per_page
int pages
Then, in order to parse the JSON into your class structure, you just need to do this:
Gson gson = new Gson();
Response response = gson.fromJson(jsonBoards, Response.class);
So you'll have all the data of all your boards into:
String nameI = response.getBoards().get(i).getName();
String titleI = response.getBoards().get(i).getTitle();
//and so on...
In java, I am trying to parse values from this json..
[
{
"2012-01-02": {
"age": 3,
"dob": "2010-01-03",
"name": "jack"
},
"2012-01-03": {
"age": 3,
"dob": "2010-01-04",
"name": "jill"
},
"2012-01-04": {
"age": 3,
"dob": "2010-01-05",
"name": "john"
},
"2012-01-05": {
"age": 3,
"dob": "2010-01-06",
"name": "miran"
}
}
]
Using JSONObject, I was trying to get the value of just "age" and then add them up to do some data manipulation.
I created a JSONObject
Created an iterator and then stored them to a map
This gets me the inner element like:
{
"age": 3,
"dob": "2010-01-06",
"name": "miran"
}
After this, not sure how to extract just age from each element. Do i create another jsonobject and pass this new string, extract age out of it or is there a better way to do this? (I am sure there is one)
UPDATE:
This is what I currently have that gives me {"age":3,"dob":"2012-01-06","name":"miran"}
JSONObject jsonobj = new JSONObject();
try {
jsonobj = new JSONObject(pastweekVol);
Iterator iter = jsonobj.keys();
Map<String, String> map = new HashMap<String, String>();
while(iter.hasNext()){
String jsonkey = (String)iter.next();
String value = jsonobj.getString(jsonkey);
logger.debug("first pass value is: {}", value);
} catch (JSONException je) {
logger.debug("exception is: {}",je);
}
I was thinking that since I am getting {"age":3,"dob":"2012-01-06","name":"miran"}, I would create another json object and pass in this string, which will give me value of "age". The problem here is that I get repetitive values. Of course, something very basic is missing here but I can't seem to figure that out.
If you have the inner element as a JSONObject instance - say person - then you can directly access the age:
int age = person.getInt("age");
and do something with it:
sum += age;
You might consider a library like Google's GSON (http://code.google.com/p/google-gson/) if you want to be able to easily parse arbitrarily complex JSON strnigs into generic objects.
Using org.json is probably not your best bet -- this API has many flaws. Using Jackson, you can easily extract age from each member value:
ObjectMapper mapper = new ObjectMapper();
JsonNode fullDocument = mapper.readTree(xxx); // xxx can be many things
// Not an object? Bail out
if (!fullDocument.isObject())
throw new IllegalArgumentException("not an object");
// This will iterate through object values
for (JsonNode value: fullDocument)
// do something with value.get("age")
// in particular, you can test for .isIntegralNumber()