{"id":1254,"date":"2014-07-09T10:00:25","date_gmt":"2014-07-09T08:00:25","guid":{"rendered":"http:\/\/www.unicoda.com\/?p=1254"},"modified":"2014-06-26T16:01:50","modified_gmt":"2014-06-26T14:01:50","slug":"java-parse-json-file","status":"publish","type":"post","link":"https:\/\/www.unicoda.com\/?p=1254","title":{"rendered":"[Java] Parse json file"},"content":{"rendered":"<p><em>Back in may 2013, I was working on a school project concerning mobile networks. So this is what I wrote one year ago while I was fighting with handover algorithm and research papers.<\/em><\/p>\n<p>I&rsquo;m currently building a project to simulate handover decision process in GSM\/UMTS. To be able to perform the simulation, I need some parameters like mobile gain, loss, coordinates as well as informations about antennas. I&rsquo;ve chose to provide these parameters to the program using a json file. So I needed to find a way to get informations from the file and parse the json in java.<\/p>\n<p>I used a <a href=\"http:\/\/json.org\/java\/\" target=\"_blank\">JSON-Java<\/a> library to initialized data. Here is the code needed to parse the json belove. The jsonStr variable contains our json file as a string.<\/p>\n<pre><span class=\"n\">JSONObject<\/span> <span class=\"n\">obj<\/span> <span class=\"o\">=<\/span> <span class=\"k\">new<\/span> <span class=\"n\">JSONObject<\/span><span class=\"o\">(<\/span><span class=\"n\">jsonStr<\/span><span class=\"o\">);<\/span>\r\n<span class=\"n\">JSONObject<\/span> <span class=\"n\">objMobile<\/span> <span class=\"o\">=<\/span> <span class=\"n\">obj<\/span><span class=\"o\">.<\/span><span class=\"na\">getJSONObject<\/span><span class=\"o\">(<\/span><span class=\"s\">\"mobile\"<\/span><span class=\"o\">);<\/span>\r\n<span class=\"n\">JSONArray<\/span> <span class=\"n\">arrayPath<\/span> <span class=\"o\">=<\/span> <span class=\"n\">objMobile<\/span><span class=\"o\">.<\/span><span class=\"na\">getJSONArray<\/span><span class=\"o\">(<\/span><span class=\"s\">\"path\"<\/span><span class=\"o\">);<\/span>\r\n\r\ncro = obj.getDouble(\"cro\");\r\nrxLevAccMin = obj.getDouble(\"rxlevaccmin\");\r\nmsTxPwrMaxCCH = obj.getDouble(\"msTxPwrMaxCCH\");\r\n\r\n\/\/Initialize Mobile Station\r\nfor(int i = 0; i &lt; arrayPath.length(); ++i) {\r\n JSONObject el = arrayPath.getJSONObject(i);\r\n journey.add(new Coordinate(el.getInt(\"x\"), el.getInt(\"y\")) );\r\n}\r\nmobileStation = new Mobile(objMobile.getDouble(\"gain\"),\r\n objMobile.getDouble(\"loss\"), objMobile.getDouble(\"classPower\"),\r\n journey, objMobile.getBoolean(\"voice\"), 6, -102, journey.get(0));\r\n\r\n\/\/Initialize Antennas\r\nJSONArray arrayAntenna = obj.getJSONArray(\"antenna\");\r\nfor(int j = 0; j &lt; arrayAntenna.length(); ++j) {\r\n JSONObject el = arrayAntenna.getJSONObject(j);\r\n Coordinate position = new Coordinate(el.getInt(\"x\"), el.getInt(\"y\"));\r\n antennaList.add(new Antenna(el.getInt(\"type\"), position, \r\n el.getDouble(\"gain\"), el.getDouble(\"loss\"), el.getDouble(\"power\"),\r\n el.getDouble(\"frequency\"), el.getDouble(\"coef\")));\r\n}<\/pre>\n<p>Once our json string has been parsed to a json object, we can extract data using:<\/p>\n<ul>\n<li>getJSONObject<\/li>\n<li>getJSONArray<\/li>\n<li>getBoolean<\/li>\n<li>getInt<\/li>\n<li>getDouble<\/li>\n<\/ul>\n<p>Here is the json file:<\/p>\n<pre>{\r\n  \"rxlevaccmin\": -96, \r\n  \"cro\": 0.0, \r\n  \"msTxPwrMaxCCH\": 33, \r\n  \"mobile\": { \r\n    \"gain\": 0, \"loss\": 0, \r\n    \"classPower\": 33, \r\n    \"voice\": false, \r\n    \"path\": [ \r\n      {\"x\": 11, \"y\": 0}, \r\n      {\"x\": 14, \"y\": 0}, \r\n      {\"x\": 16, \"y\": 0}, \r\n      {\"x\": 18, \"y\": 0}, \r\n      {\"x\": 20, \"y\": 0}, \r\n      {\"x\": 24, \"y\": 0}, \r\n      {\"x\": 25, \"y\": 0}, \r\n      {\"x\": 26, \"y\": 0} \r\n    ] \r\n  },\r\n  \"antenna\": [ \r\n    { \r\n      \"type\": 0, \r\n      \"x\": 0, \"y\": 0, \r\n      \"power\": 20, \r\n      \"gain\": 0, \"loss\": 0, \r\n      \"frequency\": 900, \r\n      \"coef\": 1 \r\n    }, \r\n    { \r\n      \"type\": 1, \r\n      \"x\": 30, \"y\": 0, \r\n      \"power\": 10, \r\n      \"gain\": 0, \"loss\": 0, \r\n      \"frequency\": 900, \r\n      \"coef\": 1 \r\n    }, \r\n    {\r\n      \"type\": 0, \r\n      \"x\": 20, \"y\": 5, \r\n      \"power\": 12, \r\n      \"gain\": 0, \"loss\": 0, \r\n      \"frequency\": 900, \r\n      \"coef\": 1 \r\n    }\r\n  ]\r\n}<\/pre>\n<p>It&rsquo;s funny to compare this with the one-line-way to parse json in Node.Js:<\/p>\n<pre>var jsonObject = require('jsonFileName');\r\nvar classPower =jsonObject.mobile.classPower<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Back in may 2013, I was working on a school project concerning mobile networks. So this is what I wrote one year ago while I was fighting with handover algorithm and research papers. I&rsquo;m currently building a project to simulate handover decision process in GSM\/UMTS. To be able to perform the simulation, I need some &hellip; <a href=\"https:\/\/www.unicoda.com\/?p=1254\" class=\"more-link\">Continuer la lecture<span class=\"screen-reader-text\"> de &laquo;&nbsp;[Java] Parse json file&nbsp;&raquo;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[62,121,122],"class_list":["post-1254","post","type-post","status-publish","format-standard","hentry","category-code","tag-java","tag-json","tag-parse"],"_links":{"self":[{"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/posts\/1254","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1254"}],"version-history":[{"count":9,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/posts\/1254\/revisions"}],"predecessor-version":[{"id":1644,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=\/wp\/v2\/posts\/1254\/revisions\/1644"}],"wp:attachment":[{"href":"https:\/\/www.unicoda.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1254"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1254"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.unicoda.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1254"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}