Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parsing a JSON array of JSON objects?

Tags:

json

arrays

go

I am trying to get each JSON object out of a JSON array. I get this data via a HTTP post.

I know what my data will look like:

   {
    "array":[
       {
          "entity_title":"University of Phoenix", 
          "entity_org_name":"CS Club",
          "possible_user_name":"Johnny Ive",
          "posibble_user_email":"[email protected]",
          "user_position_title":"President",
          "msg_body_id":4
       },
      {
          "entity_title":"University of San Francisco", 
          "entity_org_name":"Marketing club",
          "possible_user_name":"steve jobs",
          "posibble_user_email":"[email protected]",
          "user_position_title":"Student",
          "msg_body_id":5
      }
    ]
  }

My example code and my structs look like this:

    type MsgCreateUserArray struct {
         CreateUser []MsgCreateUserJson `json:"createUserArray"`
    }
    type MsgCreateUserJson struct {
        EntityTitleName string  `json:"entity_title_name"`
        EntityOrgName   string  `json:"entity_org_name"`
        PossibleUserName string `json:"possible_user_name"`
        PossibleUserEmail   string  `json:"possible_user_email"`
        UserPositionTitle   string  `json:"user_position_title"`
        MsgBodyId       string  `json:"msg_body_id, omitempty"` 
    }


func parseJson(rw http.ResponseWriter, request *http.Request) {
    decodeJson := json.NewDecoder(request.Body)

    var msg MsgCreateUserArray
    err := decodeJson.Decode(&msg)

    if err != nil {
        panic(err)
    }
    log.Println(msg.CreateUser)
}

func main() {
    http.HandleFunc("/", parseJson)
    http.ListenAndServe(":1337", nil)
}

I am not sure where how to iterate over the JSON array and get the JSON objects and then just work with the JSON objects.

like image 708
surfin Array Avatar asked Mar 29 '15 05:03

surfin Array


People also ask

How do you parse an array of JSON objects?

Use the JSON. parse() method to pase a JSON array, e.g. JSON. parse(arr) . The method parses a JSON string and returns its JavaScript value or object equivalent.

Can JSON array contain JSON objects?

Yes, json arrays can contain any valid json string: objects with different key/value pairs, other arrays, numbers, strings, booleans all in the same array.

Does JSON parse work on arrays?

JSON. parse() converts array data into a Javascript array. The array data must be a valid JSON string.


1 Answers

Try this as your structs,

type MsgCreateUserArray struct {
    CreateUser []MsgCreateUserJson `json:"array"`
}

type MsgCreateUserJson struct {
    EntityOrgName     string  `json:"entity_org_name"`
    EntityTitle       string  `json:"entity_title"`
    MsgBodyID         int64   `json:"msg_body_id,omitempty"`
    PosibbleUserEmail string  `json:"posibble_user_email"`
    PossibleUserName  string  `json:"possible_user_name"`
    UserPositionTitle string  `json:"user_position_title"`
}

Your entity_title_name is not named correctly, nor is the top level array. After you decode into a MsgCreateUserArray you can iterate over the CreateUser slice to get each MsgCreateUserJson

like image 148
sberry Avatar answered Sep 27 '22 20:09

sberry