2017-09-08 122 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"` 
}