Parsing a JSON array of JSON objects?

huangapple go评论75阅读模式
英文:

Parsing a JSON array of JSON objects?

问题

我正在尝试从JSON数组中获取每个JSON对象。我通过HTTP POST获取这些数据。

我知道我的数据的结构如下:

{
"array":[
{
"entity_title":"University of Phoenix",
"entity_org_name":"CS Club",
"possible_user_name":"Johnny Ive",
"posibble_user_email":"Johhny.Ive@uop.edu",
"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":"steven.job@uop.edu",
"user_position_title":"Student",
"msg_body_id":5
}
]
}

我的示例代码和结构如下:

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)
}

我不确定如何迭代JSON数组并获取JSON对象,然后只处理JSON对象。

英文:

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":"Johhny.Ive@uop.edu",
          "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":"steven.job@uop.edu",
          "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.

答案1

得分: 10

请尝试将以下内容作为您的结构体:

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"`
}

您的 entity_title_name 命名不正确,顶层的 array 也是如此。在解码为 MsgCreateUserArray 后,您可以迭代 CreateUser 切片以获取每个 MsgCreateUserJson

英文:

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

huangapple
  • 本文由 发表于 2015年3月29日 13:30:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/29325956.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定