2017-09-08 121 views
-1
{ 
    "meta": { 
     "type": "RESPONSE", 
     "application": "", 
     "data0": { 
     some data 
     }, 
     "lv1": [ 
     { 
     "status": "SUCCESS", 
     "response-type": "JSON", 
     "message": {}, 
     "response": { 
     "more_data": "TRUE", 
     "no_result": "5", 
     "current_page": "1", 
     "data": [[ 
     "1", 
     "2", 
     "3"]] 
     } 
     } 
    ] 
    } 
} 

type response struct { 
    META struct { 
     LV []struct { 
      RESPONSE struct { 
       Data []struct { 
        array []struct { 
         val []string 
        } 
       } `json:"data"` 
      } `json:"response"` 
     } `json:"lv1"` 
    } `json:"meta"` 

} 

我怎样才能在下面的值是多少?GO解析嵌套的JSON数组

"data": [[ 
     "1", 
     "2", 
     "3"]] 

我试过接口和结构。在接口类型[1 2 3]中使用接口结果,我不确定如何获取值。当使用结构,我试图映射阵列的与错误消息数组时遇到了问题:

“不能解组阵列分成型结构的围棋结构字段。数据{ 瓦尔斯[]串}”

+1

您发布的JSON在某处无效... – RayfenWindspear

回答

1

这是字符串数组的数组,而不是含含串结构的阵列结构的数组,所以你想要的东西更像:

type response struct { 
    Meta struct { 
     Lv []struct { 
      Response struct { 
       Data [][]string `json:"data"` 
      } `json:"response"` 
     } `json:"lv1"` 
    } `json:"meta"` 
} 

(我也改变了全部大写的字段名称匹配,期望去代码tyle)

对于它的价值,有一个方便的JSON对去工具here这给了我这个对你的输入,删除some data位后(这使得JSON无效):

type AutoGenerated struct { 
    Meta struct { 
     Type  string `json:"type"` 
     Application string `json:"application"` 
     Data0  struct { 
     } `json:"data0"` 
     Lv1 []struct { 
      Status  string `json:"status"` 
      ResponseType string `json:"response-type"` 
      Message  struct { 
      } `json:"message"` 
      Response struct { 
       MoreData string  `json:"more_data"` 
       NoResult string  `json:"no_result"` 
       CurrentPage string  `json:"current_page"` 
       Data  [][]string `json:"data"` 
      } `json:"response"` 
     } `json:"lv1"` 
    } `json:"meta"` 
}