1
votes

I have JSON text that looks like this:

{
  "ok": true,
  "totalPages": 256,
  "arReports": {
    "r2807328": {
      "marchReportId": "2807328",
      "reportUnixTime": "1332742728",
      "marchTypeState": "1"
    },
    "r2804256": {
      "marchReportId": "2804256",
      "reportUnixTime": "1332722319",
      "marchType": "4",
      "marchTypeState": "1"
    }
  },
   "arOtherNames": {
    "a455": "JL",
    "a44": "CCCP",
    "a796": "Waffenstudenten"
  }
}

I then do:

var objGW = eval('(' + s + ')');
var d = new sbt.JsonNavigator(objGW);

How do I get the list of report ids (r2807328, etc) and then the corresponding marchReportId for each?

I've tried d.stringValues('arReports') and d.stringValues('arReports/marchId') but I dont get the values as text or an array? What am I doing wrong?

2

2 Answers

0
votes

I believe you are missing the square brackets that makes your code an object look here for example -> http://www.w3schools.com/json/default.asp

0
votes

It passes the validator so I dont think it is invalid json. You can access the properties of arreports as a associative array. When working with json on the clientside you can do the following to retrieve the first element by key

d.arReports["r2807328"]

this will return the arReports object with the given key. If you want to use a property you should use

d.arReports["r2807328"].marchTypeStatus

If you want to do it on the serverside using the jsonnavigator class you can use the following syntax

var d = new sbt.JsonNavigator(json);
d.stringValues('arReports/r2807328') 

or

var d = new sbt.JsonNavigator(json);
d.stringValues('arReports/r2807328/marchTypeStatus') 

for further reference look at the following link

SBT Demo