Arezth
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-05-2017
03:14 PM
Access values after ParseJson are not in order
Hi Forum,
I was wondering if anyone had a problem while accessing the values of a ParseJason by using for each. I copy and pasted the json that was returned into jsonprettyprint and it was valid, the format was as followed (cut short, but the main point is that it was valid since it was return without problem form server, and the page was able to parse it).
And I am accessing the parsed json with the follwing code:
The problem is, when I have 4 rows, it access first the Category3 then 1,2 and 4. If I have 6 categories, it access 3, 6, 1, 5 , 2, 4 in that order. Does anyone have information or knowledge on this topic? Or am I just having code issues? As far as I can see in my code, it should be OK.
I was wondering if anyone had a problem while accessing the values of a ParseJason by using for each. I copy and pasted the json that was returned into jsonprettyprint and it was valid, the format was as followed (cut short, but the main point is that it was valid since it was return without problem form server, and the page was able to parse it).
{
"Category1": [
{
"Element1": "value1",
"Element2": "value2",
..
"ElementN": "valueN"
},
...,
{
"Element1": "value1",
"Element2": "value2",
..
"ElementN": "valueN"
}
],
"Category2": [
{
"Element1": "value1",
"Element2": "value2",
..
"ElementN": "valueN"
},
...,
{
"Element1": "value1",
"Element2": "value2",
..
"ElementN": "valueN"
}
],
"Category3": [
{
"Element1": "value1",
"Element2": "value2",
..
"ElementN": "valueN"
}
],
"Category4": [
{
"Element1": "value1",
"Element2": "value2",
..
"ElementN": "valueN"
}
]
}
And I am accessing the parsed json with the follwing code:
m.RowItems = createObject("RoSGNode","ContentNode")
json = ParseJSON(list)
for each category in json
row = createObject("RoSGNode","ContentNode")
row.Title = category
for each object in json[category]
item = createObject("RoSGNode","ContentNode")
for each itemV in object
process...
end for
row.appendChild(item)
end for
m.RowItems.appendChild(row)
end for
The problem is, when I have 4 rows, it access first the Category3 then 1,2 and 4. If I have 6 categories, it access 3, 6, 1, 5 , 2, 4 in that order. Does anyone have information or knowledge on this topic? Or am I just having code issues? As far as I can see in my code, it should be OK.
5 REPLIES 5
belltown
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-05-2017
03:37 PM
Re: Access values after ParseJson are not in order
An associative array has no order. If you look up the FOR EACH statement in the BrightScript Reference, it states:
Objects that have no intrinsic order (like AssociativeArray) are enumerated in apparent random order.
NB_
Roku Guru
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-05-2017
05:47 PM
Re: Access values after ParseJson are not in order
... if you use the .keys() method however, magic happens (keys() returns a sorted array):
for each category in json.keys()
Arezth
Visitor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2017
02:28 PM
Re: Access values after ParseJson are not in order
Thank you both for you answer, I ended up using .keys() to sort out the problem.
producer1
Reel Rookie
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2020
09:59 AM
Re: Access values after ParseJson are not in order
Help, I cannot figure out how to use the .keys method to achieve that result.
producer1
Reel Rookie
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-27-2020
10:11 AM
Re: Access values after ParseJson are not in order
How did you end up using .keys() to sort out the problem. Please post code I am having the same issue.