[Java] Parse json file

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’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’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.

I used a JSON-Java library to initialized data. Here is the code needed to parse the json belove. The jsonStr variable contains our json file as a string.

JSONObject obj = new JSONObject(jsonStr);
JSONObject objMobile = obj.getJSONObject("mobile");
JSONArray arrayPath = objMobile.getJSONArray("path");

cro = obj.getDouble("cro");
rxLevAccMin = obj.getDouble("rxlevaccmin");
msTxPwrMaxCCH = obj.getDouble("msTxPwrMaxCCH");

//Initialize Mobile Station
for(int i = 0; i < arrayPath.length(); ++i) {
 JSONObject el = arrayPath.getJSONObject(i);
 journey.add(new Coordinate(el.getInt("x"), el.getInt("y")) );
}
mobileStation = new Mobile(objMobile.getDouble("gain"),
 objMobile.getDouble("loss"), objMobile.getDouble("classPower"),
 journey, objMobile.getBoolean("voice"), 6, -102, journey.get(0));

//Initialize Antennas
JSONArray arrayAntenna = obj.getJSONArray("antenna");
for(int j = 0; j < arrayAntenna.length(); ++j) {
 JSONObject el = arrayAntenna.getJSONObject(j);
 Coordinate position = new Coordinate(el.getInt("x"), el.getInt("y"));
 antennaList.add(new Antenna(el.getInt("type"), position, 
 el.getDouble("gain"), el.getDouble("loss"), el.getDouble("power"),
 el.getDouble("frequency"), el.getDouble("coef")));
}

Once our json string has been parsed to a json object, we can extract data using:

  • getJSONObject
  • getJSONArray
  • getBoolean
  • getInt
  • getDouble

Here is the json file:

{
  "rxlevaccmin": -96, 
  "cro": 0.0, 
  "msTxPwrMaxCCH": 33, 
  "mobile": { 
    "gain": 0, "loss": 0, 
    "classPower": 33, 
    "voice": false, 
    "path": [ 
      {"x": 11, "y": 0}, 
      {"x": 14, "y": 0}, 
      {"x": 16, "y": 0}, 
      {"x": 18, "y": 0}, 
      {"x": 20, "y": 0}, 
      {"x": 24, "y": 0}, 
      {"x": 25, "y": 0}, 
      {"x": 26, "y": 0} 
    ] 
  },
  "antenna": [ 
    { 
      "type": 0, 
      "x": 0, "y": 0, 
      "power": 20, 
      "gain": 0, "loss": 0, 
      "frequency": 900, 
      "coef": 1 
    }, 
    { 
      "type": 1, 
      "x": 30, "y": 0, 
      "power": 10, 
      "gain": 0, "loss": 0, 
      "frequency": 900, 
      "coef": 1 
    }, 
    {
      "type": 0, 
      "x": 20, "y": 5, 
      "power": 12, 
      "gain": 0, "loss": 0, 
      "frequency": 900, 
      "coef": 1 
    }
  ]
}

It’s funny to compare this with the one-line-way to parse json in Node.Js:

var jsonObject = require('jsonFileName');
var classPower =jsonObject.mobile.classPower
Anonyme

Auteur/autrice : Victor

Ingénieur en informatique de formation et de métier, j’administre ce serveur et son domaine et privilégie l'utilisation de logiciels libres au quotidien. Je construis progressivement mon "cloud" personnel service après service pour conserver un certain contrôle sur mes données numériques.

Laisser un commentaire

Votre adresse e-mail ne sera pas publiée. Les champs obligatoires sont indiqués avec *