About Monkey 2 › Forums › Monkey 2 Programming Help › [Solved] can't use JsonObject.All, don't understand Iterators
This topic contains 4 replies, has 3 voices, and was last updated by
abakobo
1 year, 5 months ago.
-
AuthorPosts
-
November 13, 2017 at 10:06 am #11678
I’m trying to use the JsonObject.All method but can’t.
In the docs it says it returns an iterator but when trying to “for eachin” it, the compiler tells me it has no “atEnd” method.
So I checked Map.Iterator struct and yes it doesn’t have an “atEnd” method! is it normal? Some others iterators (stack for example) does have an “AtEnd” method.I can use eachin loops with a map getting it’s nodes within the for loop. But I actually don’t know what to do with an iterator!
I am supposed to use iterators as a end user? Am I supposed to use the JsonObject.All methods? I can get what I need with the JsonObject.ToObject() but I’d like to undesrtand a bit those things I don’t get!If there is a use of iterator, is it possible to have a small educational example.
EDIT: Forgot to call .All(), was calling .All so was receiving a function pointer…
thx
November 13, 2017 at 10:58 am #11679here’s my (working) test code for those who want a simple Json example
Monkey12345678910111213141516171819202122232425262728293031323334353637383940414243444546Namespace myapp#Import "<std>"Using std..Function Main()ChangeDir("c:/mx2_short/json")Local arr:=New String[4]arr[0]="0"arr[1]="1"arr[2]="2"arr[3]="3"Local jobj:=New JsonObjectLocal kobj:=New JsonObjectLocal jarr:=New JsonArrayFor Local str:=Eachin arrjarr.Add( New JsonString( str ) )Nextjobj["arrai"]=jarrjobj["i"]=New JsonNumber(7)jobj["j"]=New JsonNumber(8)kobj["a"]=New JsonString("haha")kobj["o"]=New JsonString("hoho")jobj["hihi"]=kobjSaveString( jobj.ToJson(),"test3.json" )Local lobj:=JsonObject.Load( "test3.json" )'Local objmap:=lobj.ToObject()Local JOAll:=jobj.All()While JOAll.Current <> NullPrint JOAll.Current.KeyJOAll.Bump()WendEndNovember 13, 2017 at 3:04 pm #11681That’s the risk of using := I guess.
November 14, 2017 at 6:11 am #11692Some additional iteration cases:
Monkey12345678910111213141516171819202122232425262728Namespace myapp#Import "<std>"Using std..Function Main()Local json:=New JsonObjectjson["hello"]=New JsonString( "Hello, " )json["world"]=New JsonString( "world!" )' case 1 - using Map.Node<String,JsonValue>For Local node:=Eachin jsonPrint "1: "+node.Key+" : "+node.Value.ToString()Next' case 2 - using Map.Keys, key is String hereFor Local key:=Eachin json.Data.KeysPrint "2: "+key+" : "+json[key].ToString()Next' case 3 - using Map.Values, val is JsonValue hereFor Local val:=Eachin json.Data.ValuesPrint "3: "+val.ToString()NextEndNovember 14, 2017 at 11:23 am #11696Thanks nerobot!
-
AuthorPosts
You must be logged in to reply to this topic.