0

I'm sending the following JSON to a Java Program. I put in this configuration so that my order would be preserved. I'm using the org.json library.

"Ordered JSON"

    var obj = {
        "params":
        [
            {"base_s" : robot.base.length},
            {"base_k" : robot.base.k},
            {"base_phi" : robot.base.phi},
            {"mid_s" : robot.mid.length},
            {"mid_k" : robot.mid.k},
            {"mid_phi" : robot.mid.phi},
            {"tip_s" : robot.tip.length},
            {"tip_k" : robot.tip.k},
            {"tip_phi" : robot.tip.phi}
        ]
    };

Before I had the JSON formatted as such: Original JSON

    var obj = {
        base_s : robot.base.length,
        base_k : robot.base.k,
        base_phi : robot.base.phi,
        mid_s : robot.mid.length,
        mid_k : robot.mid.k,
        mid_phi : robot.mid.phi,
        tip_s : robot.tip.length,
        tip_k : robot.tip.k,
        tip_phi : robot.tip.phi
    };

The following Java code interpreted the original JSON fine, but has issues with the "Ordered" JSON.

    JSONArray nameArray = jsonData.names();
    JSONArray valArray = jsonData.toJSONArray(nameArray);

The nameArray is ["items"] and the valArray is [[{"base_s":0.314},{"base_k":0.0012},{"base_phi":0.436},{"mid_s":0.314},{"mid_k":0.0012},{"mid_phi":0.436},{"tip_s":0.3139},{"tip_k":0.0012},{"tip_phi":0.436}]]

I'm curious as to how I "do this one more time" in order to extract the nine numbers out of the array. So that I get something like this vals = [0.314, 0.0012, 0.436, 0.314, 0.0012, 0.436, 0.3139, 0.0012, 0.436] that I can then turn into an array of floats.

ryanmattscott
  • 321
  • 5
  • 17
  • 1
    Why mangle your data to force an order that is meaningless? They are name-value pairs, and you access them by name, so order is unimportant. If your code relies on order, then your code is wrong, not the data. – Andreas May 24 '16 at 20:09
  • That wasn't the question. Then how would I access them by name? I suppose the order itself doesn't matter but I do need to know what is what so to say. So I suppose I could just do a painful localeCompare sequence to put it in a more sensible order. They're robot joint positions so it makes sense to acess them in order.... – ryanmattscott May 24 '16 at 20:20
  • You call `jsonData.getDouble("base_s")`, `jsonData.getDouble("base_k")`, and so on. – Andreas May 24 '16 at 20:44
  • Just to clarify: Are we to assume you are using the Android JSON library? You should always specify what library you are using, since there are many JSON libraries for Java. – Andreas May 24 '16 at 20:48
  • Thanks I wasn't aware of `jsonData.getDouble("base_s")` I'm using the org.json library. – ryanmattscott May 24 '16 at 21:45

1 Answers1

1

For the ordered JSON you can try something like the following

JSONArray results = obj.getJsonArray("params");
for (int i = 0, size = results.length(); i < size; i++){
  JSONObject objectInArray = results.getJSONObject(i);
  String[] elementNames = JSONObject.getNames(objectInArray);
  for (String elementName : elementNames) {
    String value = objectInArray.getString(elementName); 
    // in value you have your double
  }
}
ryanmattscott
  • 321
  • 5
  • 17
Hooch
  • 487
  • 3
  • 11
  • You wrote `JsonArray`, but question said `JSONArray`. Are you referencing a different library? – Andreas May 24 '16 at 20:41
  • I'm referencing to javax.json,JsonArray [link](https://docs.oracle.com/javaee/7/api/javax/json/JsonArray.html). Which library are you using? – Hooch May 24 '16 at 20:46
  • *I'm* not using anything, but I think the question is using the Android library, since that fits the methods used in the question. – Andreas May 24 '16 at 20:47
  • Sorry that isn't very clear. By the way seems that the method of JSONArray and JSONObject of android are the same of javax. [Andorid JSONArray](https://developer.android.com/reference/org/json/JSONArray.html) – Hooch May 24 '16 at 20:53
  • How do you figure that? `JsonArray` and `JSONArray` is not the same class (Java is case-sensitive, remember?), and the `names()` method used in the question doesn't even exist in the javax version. I agree that it isn't clear, because question doesn't say which library is used, but an answer that is clearly using a different library (`JsonArray` vs `JSONArray`), is not a useful answer. – Andreas May 24 '16 at 20:55
  • Yes java is case sensitive and import statement matters too... By the way I've not figured anything, I've just looked up the documentation in the link I've provided above. Just keep in mind that if you are on android [you can't use javax](http://stackoverflow.com/questions/16803343/javax-cannot-be-imported-in-my-android-app) – Hooch May 24 '16 at 20:59