Parsing as JSON Array

Former Member
Former Member
Hi,

I modified the Weather sample code to call my own personal API and return the following array of objects. I cannot work out how to iterate through the JSON and parse to a local list/array. Do you have any examples please?

[
{
"Name":"Morning Ride",
"TrainingLoad":45,
"TypeId":1
},
{
"Name":"Night Run - getting quicker now, despite being so blind as to actually run round a corner in Hyde Park and go the wrong way. ",
"TrainingLoad":78,
"TypeId":2
},
{
"Name":"I could not be wetter",
"TrainingLoad":23,
"TypeId":1
}
]
  • Former Member
    Former Member over 9 years ago
    The data you get in your response call back would a 3 element array where the elements are the hashes listed.

    The data points are:
    data[0]["Name"]
    data[0]["TrainingLoad"]
    data[0]["TypeId"]
    data[1]["Name"]
    data[1]["TrainingLoad"]
    data[1]["TypeId"]
    data[2]["Name"]
    data[2]["TrainingLoad"]
    data[2]["TypeId"]
  • Every time you see an opening bracket ([), you are looking at an Array. Every time you see an opening brace ({) you are looking at a Dictionary. For the following JSON data...

    [
    {
    "Name":"Morning Ride",
    "TrainingLoad":45,
    "TypeId":1
    },
    {
    "Name":"Night Run - getting quicker now, despite being so blind as to actually run round a corner in Hyde Park and go the wrong way. ",
    "TrainingLoad":78,
    "TypeId":2
    },
    {
    "Name":"I could not be wetter",
    "TrainingLoad":23,
    "TypeId":1
    }
    ]


    You would parse it like this...

    function onJsonData(code, data) {

    // error check using `code' omitted
    // error check verifying `data' is not null omitted

    // the outer element is an array, iterate over the elements.
    for (var i = 0; i < data.size(); ++i) {
    var entry = data;

    // each entry in the array is a dictionary. lookup fields by name.
    var name = entry["Name"]; // or entry.get("Name")
    var load = entry["TrainingLoad"]; // ...
    var type = entry["TypeId"]; // ...
    }
    }
    [/code]
  • Former Member
    Former Member over 9 years ago
    Thanks Travis. Yep, that did the trick. My background is C#, so this is all pretty alien to me.

    This is how implemented it.

    http://stevessoftwareprojects.com/strava-apps/connectiq-monkey-c-test-project-1-a-simple-activity-feed/



    Every time you see an opening bracket ([), you are looking at an Array. Every time you see an opening brace ({) you are looking at a Dictionary. For the following JSON data...

    [
    {
    "Name":"Morning Ride",
    "TrainingLoad":45,
    "TypeId":1
    },
    {
    "Name":"Night Run - getting quicker now, despite being so blind as to actually run round a corner in Hyde Park and go the wrong way. ",
    "TrainingLoad":78,
    "TypeId":2
    },
    {
    "Name":"I could not be wetter",
    "TrainingLoad":23,
    "TypeId":1
    }
    ]


    You would parse it like this...

    function onJsonData(code, data) {

    // error check using `code' omitted
    // error check verifying `data' is not null omitted

    // the outer element is an array, iterate over the elements.
    for (var i = 0; i < data.size(); ++i) {
    var entry = data;

    // each entry in the array is a dictionary. lookup fields by name.
    var name = entry["Name"]; // or entry.get("Name")
    var load = entry["TrainingLoad"]; // ...
    var type = entry["TypeId"]; // ...
    }
    }
    [/code]