Quantcast
Channel: SCN : All Content - RESTful Web Services SDK
Viewing all articles
Browse latest Browse all 262

JSON arrays vs. properties

$
0
0

I'm finding that a JSON response from Raylight may or may not be an array depending on whether there are multiple items.  For example, a WebI doc with multiple reports will produce this:

 

{

  "reports": {

    "report": [

      {

        "id": 2,

        "name": "Report 1",

        "reference": "2.RS",

        "showDataChanges": false

      },

      {

        "id": 1,

        "name": "Report 2",

        "reference": "1.RS",

        "showDataChanges": false

      }

    ]

  }

}

 

Whereas if there is only one report tab, the result is:

 

{

  "reports": {

    "report":  {

        "id": 2,

        "name": "Report 1",

        "reference": "2.RS",

        "showDataChanges": false

      }

  }

}

 

This is a problem, since the report property must be accessed differently.  With JQuery, if "report" is an array, I can access the individual reports with:

$.each(json.reports.report,function() { $(this).id });

 

But the code doesn't work if there is only one report -- it returns the properties of the "report" property instead of report itself.  Instead, I have to do:

json.reports.report.id

 

So, is there any code that can access the "report" properties whether or not it's an array?


Viewing all articles
Browse latest Browse all 262

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>