json parse problem
On Sat, 16 Mar 2019 at 04:21, Roelof Wobben <r.wobben@home.nl> wrote:
Hello,
I try to parse a json that differs some times
after parsing with neojson I see something like this
"an Array(a Dictionary('height'->3640 'name'->'z0' 'tiles'->an Array(a Dictionary('url'->'http://lh3...TItQ4wwXk80sCHkqSS3JgTgmehYP-OItkO4hMwqH1agr-LfNRnxUCRSJKc_VQ=s...' 'x'->0 'y'->0 )) 'width'->404 ))"
is there a way I can filter out a dictonary where the name is equal to z4
Because of cultural differences, I'm not sure by "filter out" whether you mean include or exclude but when filtering any collection type, #select: #detect and #reject: are you main go to messages. If you inspect your dictionary and on the meta tab filter for ...*select*... you'll find #associationsSelect: which works like this... d := Dictionary newFromPairs: #( 1 2 3 4 5 6 ). d associationsSelect: [ :asn | (asn key = 1) and: (asn value = 2) ] d inspect. ==> Dictionary [1 item] (1->2 ) I notice there is not a similar #associationsReject: so if needed you could add your own considering... d := Dictionary newFromPairs: #( 1 2 3 4 5 6 ). d associations select: [ :asn | (asn key = 1) and: (asn value = 2) ] d inspect. ==> Array [1 item] (1->2) d := Dictionary newFromPairs: #( 1 2 3 4 5 6 ). d associations reject: [ :asn | (asn key = 1) and: (asn value = 2) ] d inspect. ==> Array [2 items] (5->6 3->4) cheers -ben
On Sat, 16 Mar 2019 at 17:05, Roelof Wobben <r.wobben@home.nl> wrote:
Thanks,
With your hint I solved it this way :
imageUrl1 := levelsJson select: [ :item | (item at: #name) = 'z4' ]. imageUrl := (((imageUrl1 at: 1) at: #tiles) at: 1) at: #url
Cool. Minor tweak... if you use #detect: instead of #select: which returns the first matched item rather than a collection so you wont need ....(imageUrl1 at: 1).... part. cheers -ben
Roelof
Op 16-3-2019 om 01:04 schreef Ben Coman:
On Sat, 16 Mar 2019 at 04:21, Roelof Wobben <r.wobben@home.nl> wrote:
Hello,
I try to parse a json that differs some times
after parsing with neojson I see something like this
"an Array(a Dictionary('height'->3640 'name'->'z0' 'tiles'->an Array(a Dictionary('url'->'http://lh3...TItQ4wwXk80sCHkqSS3JgTgmehYP-OItkO4hMwqH1agr-LfNRnxUCRSJKc_VQ=s...' 'x'->0 'y'->0 )) 'width'->404 ))"
is there a way I can filter out a dictonary where the name is equal to z4
Because of cultural differences, I'm not sure by "filter out" whether you mean include or exclude but when filtering any collection type, #select: #detect and #reject: are you main go to messages. If you inspect your dictionary and on the meta tab filter for ...*select*... you'll find #associationsSelect: which works like this...
d := Dictionary newFromPairs: #( 1 2 3 4 5 6 ). d associationsSelect: [ :asn | (asn key = 1) and: (asn value = 2) ] d inspect. ==> Dictionary [1 item] (1->2 )
I notice there is not a similar #associationsReject: so if needed you could add your own considering... d := Dictionary newFromPairs: #( 1 2 3 4 5 6 ). d associations select: [ :asn | (asn key = 1) and: (asn value = 2) ] d inspect. ==> Array [1 item] (1->2)
d := Dictionary newFromPairs: #( 1 2 3 4 5 6 ). d associations reject: [ :asn | (asn key = 1) and: (asn value = 2) ] d inspect. ==> Array [2 items] (5->6 3->4)
cheers -ben
On Sat, 16 Mar 2019 at 23:29, Roelof Wobben <r.wobben@home.nl> wrote:
oke,
how do I then get the url of the line where the name is z4.
I haven't directly tested, but probably... imageUrl1 := levelsJson detect: [ :item | (item at: #name) = 'z4' ]. imageUrl := ((imageUrl1 at: #tiles) at: 1) at: #url cheers -ben I need that because I need that line and then the tiles part which is also
a collection and then the url part which also is a part of a collection
Roelof
Op 16-3-2019 om 16:24 schreef Ben Coman:
On Sat, 16 Mar 2019 at 17:05, Roelof Wobben <r.wobben@home.nl> wrote:
Thanks,
With your hint I solved it this way :
imageUrl1 := levelsJson select: [ :item | (item at: #name) = 'z4' ]. imageUrl := (((imageUrl1 at: 1) at: #tiles) at: 1) at: #url
Cool. Minor tweak... if you use #detect: instead of #select: which returns the first matched item rather than a collection so you wont need ....(imageUrl1 at: 1).... part.
cheers -ben
Roelof
Op 16-3-2019 om 01:04 schreef Ben Coman:
On Sat, 16 Mar 2019 at 04:21, Roelof Wobben <r.wobben@home.nl> wrote:
Hello,
I try to parse a json that differs some times
after parsing with neojson I see something like this
"an Array(a Dictionary('height'->3640 'name'->'z0' 'tiles'->an Array(a Dictionary('url'->'http://lh3...TItQ4wwXk80sCHkqSS3JgTgmehYP-OItkO4hMwqH1agr-LfNRnxUCRSJKc_VQ=s...' 'x'->0 'y'->0 )) 'width'->404 ))"
is there a way I can filter out a dictonary where the name is equal to z4
Because of cultural differences, I'm not sure by "filter out" whether you mean include or exclude but when filtering any collection type, #select: #detect and #reject: are you main go to messages. If you inspect your dictionary and on the meta tab filter for ...*select*... you'll find #associationsSelect: which works like this...
d := Dictionary newFromPairs: #( 1 2 3 4 5 6 ). d associationsSelect: [ :asn | (asn key = 1) and: (asn value = 2) ] d inspect. ==> Dictionary [1 item] (1->2 )
I notice there is not a similar #associationsReject: so if needed you could add your own considering... d := Dictionary newFromPairs: #( 1 2 3 4 5 6 ). d associations select: [ :asn | (asn key = 1) and: (asn value = 2) ] d inspect. ==> Array [1 item] (1->2)
d := Dictionary newFromPairs: #( 1 2 3 4 5 6 ). d associations reject: [ :asn | (asn key = 1) and: (asn value = 2) ] d inspect. ==> Array [2 items] (5->6 3->4)
cheers -ben
On 16 Mar 2019, at 16:32, Ben Coman <btc@openinworld.com> wrote:
On Sat, 16 Mar 2019 at 23:29, Roelof Wobben <r.wobben@home.nl> wrote: oke,
how do I then get the url of the line where the name is z4.
I haven't directly tested, but probably... imageUrl1 := levelsJson detect: [ :item | (item at: #name) = 'z4' ]. imageUrl := ((imageUrl1 at: #tiles) at: 1) at: #url
If you are not using mapping, you get Dictionary and Array instances back. You can substitute Dictionary for NeoJSONObject, which is a Dictionary subclass that behaves more like a JavaScript object (which is good or bad, depending on your point of view). obj := NeoJSONObject fromString: '{"foo":1,"bar":-2}'. Given the above, you can do obj foo obj bar It is as if each key has an accessor (and mutator as well). If there is no such key, nil is returned, duh. So you could write Ben's code as imageUrl1 := levelsJson detect: [ :item | item name = 'z4' ]. imageUrl := imageUrl1 tiles first url. Which you might like more.
cheers -ben
I need that because I need that line and then the tiles part which is also a collection and then the url part which also is a part of a collection
Roelof
Op 16-3-2019 om 16:24 schreef Ben Coman:
On Sat, 16 Mar 2019 at 17:05, Roelof Wobben <r.wobben@home.nl> wrote: Thanks,
With your hint I solved it this way :
imageUrl1 := levelsJson select: [ :item | (item at: #name) = 'z4' ]. imageUrl := (((imageUrl1 at: 1) at: #tiles) at: 1) at: #url
Cool. Minor tweak... if you use #detect: instead of #select: which returns the first matched item rather than a collection so you wont need ....(imageUrl1 at: 1).... part.
cheers -ben
Roelof
Op 16-3-2019 om 01:04 schreef Ben Coman:
On Sat, 16 Mar 2019 at 04:21, Roelof Wobben <r.wobben@home.nl> wrote: Hello,
I try to parse a json that differs some times
after parsing with neojson I see something like this
"an Array(a Dictionary('height'->3640 'name'->'z0' 'tiles'->an Array(a Dictionary('url'->'http://lh3...TItQ4wwXk80sCHkqSS3JgTgmehYP-OItkO4hMwqH1agr-LfNRnxUCRSJKc_VQ=s... ' 'x'->0 'y'->0 )) 'width'->404 ))"
is there a way I can filter out a dictonary where the name is equal to z4
Because of cultural differences, I'm not sure by "filter out" whether you mean include or exclude but when filtering any collection type, #select: #detect and #reject: are you main go to messages. If you inspect your dictionary and on the meta tab filter for ...*select*... you'll find #associationsSelect: which works like this...
d := Dictionary newFromPairs: #( 1 2 3 4 5 6 ). d associationsSelect: [ :asn | (asn key = 1) and: (asn value = 2) ] d inspect. ==> Dictionary [1 item] (1->2 )
I notice there is not a similar #associationsReject: so if needed you could add your own considering... d := Dictionary newFromPairs: #( 1 2 3 4 5 6 ). d associations select: [ :asn | (asn key = 1) and: (asn value = 2) ] d inspect. ==> Array [1 item] (1->2)
d := Dictionary newFromPairs: #( 1 2 3 4 5 6 ). d associations reject: [ :asn | (asn key = 1) and: (asn value = 2) ] d inspect. ==> Array [2 items] (5->6 3->4)
cheers -ben
On 16 Mar 2019, at 21:17, Roelof Wobben <r.wobben@home.nl> wrote:
Op 16-3-2019 om 18:16 schreef Sven Van Caekenberghe:
imageUrl1 := levelsJson detect: [ :item | item name = 'z4' ]. imageUrl := imageUrl1 tiles first url.
nope,
when I do this I see this error messsage :
MessageNotUnderstood: Dictionary>>name
Roelof
Yes, but you have to parse your JSON differently, either doing NeoJSONObject fromString: .. or, alternatively you have to set the #mapClass, like this (NeoJSONReader on: string readStream) mapClass: NeoJSONObject; propertyNamesAsSymbols: true; next. I don't know how you invoke NeoJSON right now, but you have to change that slightly.
Op 16-3-2019 om 22:24 schreef Sven Van Caekenberghe:
imageUrl1 := levelsJson detect: [ :item | item name = 'z4' ]. imageUrl := imageUrl1 tiles first url. Thanks
I used NeoJsonReader. After chancing it to NeoJsonObject It worked. I think I can do the same here : json := NeoJSONReader fromString: (ZnEasy get: url) contents. Â Â Â artObjectJson := json at: #artObject. Â Â Â painter := ((artObjectJson at: #principalMakers) at: 1) at: #name. Â Â Â title := artObjectJson at: #title. and here (json at: #artObjects) Â Â Â Â Â Â do: [ :eachArtObject | Â Â Â Â Â Â Â Â Â | painting | Â Â Â Â Â Â Â Â Â painting := Painting new. Â Â Â Â Â Â Â Â Â painting objectNumber: (eachArtObject at: #objectNumber). Â Â Â Â Â Â Â Â Â paintings addPainting: painting ]. Â Â Â ^ paintings I will try that tomorrow. Now time to sleep here Roelof
participants (3)
-
Ben Coman -
Roelof Wobben -
Sven Van Caekenberghe