I need to send an Array inside a JSON object with the structure:
{"extent":[-76.0624694824, 36.8856620774, -75.9800720215,36.9449529607]}
How would I do this? I can't use the typical:
var jsonprep string = `{"extent":` + []float32{-76.0624694824, 36.8856620774, -75.9800720215, 36.9449529607} + `}`
var jsonStr = []byte(jsonprep)
because of the type mismatch. I am trying to send this to a server that expects it to be an array as I get the error,
The request content was malformed:
Expected List as JsArray, but got "[-76.0624694824, 36.8856620774, -75.9800720215,36.9449529607]"
If you are expecting that the array will change at some point, consider using the encoding/json package
Then you can create a struct prototype of your JSON object, then use json.Marshal() to serialize it into a []byte representation of the JSON object for transfer (whether by stdio, tcp, whatever).
e.g.
type ExampleJSON struct {
Extent []float32 `json:"extent"`
}
func main(){
var ex := &ExampleJSON{
[]float32{-76.0624694824, 36.8856620774, -75.9800720215, 36.9449529607}
}
jsonBytes, err := json.Marshal(ex)
if err != nil {
//...
}
}
I was overthinking it. The answer was:
var jsonprep string = `{"extent":[-76.0624694824, 36.8856620774, -75.9800720215, 36.9449529607]}`