8

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.

3
  • Do you get any error? Can you post it ? Commented Mar 29, 2015 at 5:52
  • no, I get the response it looks like this: 2015/03/29 01:01:14 [{University of Phoenix CS Club Johnny Ive [email protected] President } {University of San Francisco Marketing club steve jobs [email protected] Student }] Commented Mar 29, 2015 at 6:03
  • Field names don't match (e.g. you have json:"entity_title_name" but your json has "entitiy_title", and createUserArray vs array); some fields have the wrong type (e.g. your MsgBodyId string but the JSON has a number; struct tags shouldn't contain spaces (e.g. remove the space from json:"msg_body_id, omitempty") … etc … Commented Mar 29, 2015 at 6:09

1 Answer 1

10

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

Sign up to request clarification or add additional context in comments.

Comments

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.