Closed muhammad-arif closed 2 years ago
func ExampleArray() {
if !SupportedCPU() {
// Fake it
fmt.Println("Found array\nType: int value: 116\nType: int value: 943\nType: int value: 234\nType: int value: 38793")
return
}
input := `{
"Image":
{
"Animated": false,
"Height": 600,
"IDs":
[
116,
943,
234,
38793
],
"Thumbnail":
{
"Height": 125,
"Url": "http://www.example.com/image/481989943",
"Width": 100
},
"Title": "View from 15th Floor",
"Width": 800
},
"Alt": "Image of city"
}`
pj, err := Parse([]byte(input), nil)
if err != nil {
log.Fatal(err)
}
i := pj.Iter()
i.AdvanceInto()
// Grab root
_, root, err := i.Root(nil)
if err != nil {
log.Fatal(err)
}
// Grab top object
obj, err := root.Object(nil)
if err != nil {
log.Fatal(err)
}
// Find element in path.
elem, err := obj.FindPath(nil, "Image", "IDs")
if err != nil {
log.Fatal(err)
}
fmt.Println("Found", elem.Type)
if elem.Type == TypeArray {
array, err := elem.Iter.Array(nil)
if err != nil {
log.Fatal(err)
}
array.ForEach(func(i Iter) {
asString, _ := i.StringCvt()
fmt.Println("Type:", i.Type(), "value:", asString)
})
}
//Output:
//Found array
//Type: int value: 116
//Type: int value: 943
//Type: int value: 234
//Type: int value: 38793
}
You have an excellent project and documentation here. But due to my limited knowledge and experience, I am struggling to understand a few concepts. In the following example, you have shown how to find a path here
What I am strugling to understand is that, the path you are choosing (Image -> Thumbnail -> Url) is an object but if it is an array how can I decipher it. For example, if I want to find the falue of
IDs
(eg,elem, err := obj.FindPath(nil, "Image", "IDs"
) I cannot convert it to a String or bytearray. Would you add an example in the doc or here how can I parse the array in this example? Thanks