Skip to content

Commit e7da8e4

Browse files
authored
Add Array example (#67)
1 parent b118471 commit e7da8e4

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

parsed_object_test.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,74 @@ func ExampleObject_FindPath() {
191191
// string
192192
// http://www.example.com/image/481989943 <nil>
193193
}
194+
195+
func ExampleArray() {
196+
if !SupportedCPU() {
197+
// Fake it
198+
fmt.Println("Found array\nType: int value: 116\nType: int value: 943\nType: int value: 234\nType: int value: 38793")
199+
return
200+
}
201+
input := `{
202+
"Image":
203+
{
204+
"Animated": false,
205+
"Height": 600,
206+
"IDs":
207+
[
208+
116,
209+
943,
210+
234,
211+
38793
212+
],
213+
"Thumbnail":
214+
{
215+
"Height": 125,
216+
"Url": "http://www.example.com/image/481989943",
217+
"Width": 100
218+
},
219+
"Title": "View from 15th Floor",
220+
"Width": 800
221+
},
222+
"Alt": "Image of city"
223+
}`
224+
pj, err := Parse([]byte(input), nil)
225+
if err != nil {
226+
log.Fatal(err)
227+
}
228+
i := pj.Iter()
229+
i.AdvanceInto()
230+
231+
// Grab root
232+
_, root, err := i.Root(nil)
233+
if err != nil {
234+
log.Fatal(err)
235+
}
236+
// Grab top object
237+
obj, err := root.Object(nil)
238+
if err != nil {
239+
log.Fatal(err)
240+
}
241+
242+
// Find element in path.
243+
elem, err := obj.FindPath(nil, "Image", "IDs")
244+
if err != nil {
245+
log.Fatal(err)
246+
}
247+
fmt.Println("Found", elem.Type)
248+
if elem.Type == TypeArray {
249+
array, err := elem.Iter.Array(nil)
250+
if err != nil {
251+
log.Fatal(err)
252+
}
253+
array.ForEach(func(i Iter) {
254+
asString, _ := i.StringCvt()
255+
fmt.Println("Type:", i.Type(), "value:", asString)
256+
})
257+
}
258+
//Output:
259+
//Found array
260+
//Type: int value: 116
261+
//Type: int value: 943
262+
//Type: int value: 234
263+
//Type: int value: 38793
264+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy