0

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]"

2 Answers 2

2

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 {
        //...
    }
}
Sign up to request clarification or add additional context in comments.

Comments

-1

I was overthinking it. The answer was:

var jsonprep string = `{"extent":[-76.0624694824, 36.8856620774, -75.9800720215, 36.9449529607]}`

2 Comments

this isn't valid. Please wrap those numbers in square brackets ([]). Also, while you can statically write out the strings, you should look into using the encoding/json package for this stuff.
I was using it from r.PostFormValue("extent") so I just used those numbers as a stand in. In reality I used var jsonprep string = {"extent": + r.PostFormValue("extent") + }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.