File tree 1 file changed +71
-0
lines changed 1 file changed +71
-0
lines changed Original file line number Diff line number Diff line change @@ -191,3 +191,74 @@ func ExampleObject_FindPath() {
191
191
// string
192
192
// http://www.example.com/image/481989943 <nil>
193
193
}
194
+
195
+ func ExampleArray () {
196
+ if ! SupportedCPU () {
197
+ // Fake it
198
+ fmt .Println ("Found array\n Type: int value: 116\n Type: int value: 943\n Type: int value: 234\n Type: 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
+ }
You can’t perform that action at this time.
0 commit comments