2017-10-21 101 views
-1

我有一個看起來像這樣的JSON對象。它包含一週三餐,總計21項,因爲每餐都是個人入場。Golang自定義解組嵌套JSON

{ 
    "name": "MealPlan 1508620645147", 
    "items": [ 
     { 
      "day": 1, 
      "mealPlanId": 0, 
      "slot": 1, 
      "position": 0, 
      "type": "RECIPE", 
      "value": "{\"id\":869953,\"imageType\":\"jpg\",\"title\":\"Cream Cheese & Fruit Breakfast Pastries\"}" 
     }, 
     { 
      "day": 1, 
      "mealPlanId": 0, 
      "slot": 2, 
      "position": 0, 
      "type": "RECIPE", 
      "value": "{\"id\":537176,\"imageType\":\"jpg\",\"title\":\"Leftover Rice Casserole\"}" 
     }, 
     { 
      "day": 1, 
      "mealPlanId": 0, 
      "slot": 3, 
      "position": 0, 
      "type": "RECIPE", 
      "value": "{\"id\":595927,\"imageType\":\"jpg\",\"title\":\"Spinach and Cheddar Quiche\"}" 
     }, 
     { 
      "day": 2, 
      "mealPlanId": 0, 
      "slot": 1, 
      "position": 0, 
      "type": "RECIPE", 
      "value": "{\"id\":536716,\"imageType\":\"jpg\",\"title\":\"Candied Pecan Waffles\"}" 
     }, 
     { 
      "day": 2, 
      "mealPlanId": 0, 
      "slot": 2, 
      "position": 0, 
      "type": "RECIPE", 
      "value": "{\"id\":893265,\"imageType\":\"jpg\",\"title\":\"Tahini Date Smoothie Bowls\"}" 
     }, 
     { 
      "day": 2, 
      "mealPlanId": 0, 
      "slot": 3, 
      "position": 0, 
      "type": "RECIPE", 
      "value": "{\"id\":512880,\"imageType\":\"jpg\",\"title\":\"Grilled Caprese Salad Sandwich for #SundaySupper\"}" 
     }, 
     { 
      "day": 3, 
      "mealPlanId": 0, 
      "slot": 1, 
      "position": 0, 
      "type": "RECIPE", 
      "value": "{\"id\":648647,\"imageType\":\"jpg\",\"title\":\"Jumbo Blueberry Muffins\"}" 
     }, 
     { 
      "day": 3, 
      "mealPlanId": 0, 
      "slot": 2, 
      "position": 0, 
      "type": "RECIPE", 
      "value": "{\"id\":548554,\"imageType\":\"jpg\",\"title\":\"Brie, Pesto, and Sweet Pepper Grilled Cheese\"}" 
     }, 
     { 
      "day": 3, 
      "mealPlanId": 0, 
      "slot": 3, 
      "position": 0, 
      "type": "RECIPE", 
      "value": "{\"id\":438024,\"imageType\":\"jpg\",\"title\":\"Mother's Manicotti\"}" 
     }, 
     { 
      "day": 4, 
      "mealPlanId": 0, 
      "slot": 1, 
      "position": 0, 
      "type": "RECIPE", 
      "value": "{\"id\":681594,\"imageType\":\"jpg\",\"title\":\"Huevos Rancheros\"}" 
     }, 
     { 
      "day": 4, 
      "mealPlanId": 0, 
      "slot": 2, 
      "position": 0, 
      "type": "RECIPE", 
      "value": "{\"id\":99184,\"imageType\":\"jpg\",\"title\":\"Black Bean Tacos\"}" 
     }, 
     { 
      "day": 4, 
      "mealPlanId": 0, 
      "slot": 3, 
      "position": 0, 
      "type": "RECIPE", 
      "value": "{\"id\":604514,\"imageType\":\"jpg\",\"title\":\"Cheddar Scallion Dutch Baby\"}" 
     }, 
     { 
      "day": 5, 
      "mealPlanId": 0, 
      "slot": 1, 
      "position": 0, 
      "type": "RECIPE", 
      "value": "{\"id\":622672,\"imageType\":\"jpg\",\"title\":\"Cinnamon-Sugar Streusel Baked French Toast\"}" 
     }, 
     { 
      "day": 5, 
      "mealPlanId": 0, 
      "slot": 2, 
      "position": 0, 
      "type": "RECIPE", 
      "value": "{\"id\":636178,\"imageType\":\"jpg\",\"title\":\"Broccoli Cheddar Soup, A Panera Bread Co. Copycat\"}" 
     }, 
     { 
      "day": 5, 
      "mealPlanId": 0, 
      "slot": 3, 
      "position": 0, 
      "type": "RECIPE", 
      "value": "{\"id\":452482,\"imageType\":\"jpg\",\"title\":\"Slow Cooker Macaroni and Cheese I\"}" 
     }, 
     { 
      "day": 6, 
      "mealPlanId": 0, 
      "slot": 1, 
      "position": 0, 
      "type": "RECIPE", 
      "value": "{\"id\":157272,\"imageType\":\"jpg\",\"title\":\"Pomegranate-Nutella Waffles\"}" 
     }, 
     { 
      "day": 6, 
      "mealPlanId": 0, 
      "slot": 2, 
      "position": 0, 
      "type": "RECIPE", 
      "value": "{\"id\":619111,\"imageType\":\"jpg\",\"title\":\"Barley, Bulgur and Vegetable Vegan Casserole\"}" 
     }, 
     { 
      "day": 6, 
      "mealPlanId": 0, 
      "slot": 3, 
      "position": 0, 
      "type": "RECIPE", 
      "value": "{\"id\":510089,\"imageType\":\"jpg\",\"title\":\"Stovetop Mac and Cheese\"}" 
     }, 
     { 
      "day": 7, 
      "mealPlanId": 0, 
      "slot": 1, 
      "position": 0, 
      "type": "RECIPE", 
      "value": "{\"id\":551869,\"imageType\":\"jpg\",\"title\":\"Berry Smoothie\"}" 
     }, 
     { 
      "day": 7, 
      "mealPlanId": 0, 
      "slot": 2, 
      "position": 0, 
      "type": "RECIPE", 
      "value": "{\"id\":590452,\"imageType\":\"jpg\",\"title\":\"Cheesy Baked Pasta with Eggplant and Artichokes\"}" 
     }, 
     { 
      "day": 7, 
      "mealPlanId": 0, 
      "slot": 3, 
      "position": 0, 
      "type": "RECIPE", 
      "value": "{\"id\":590452,\"imageType\":\"jpg\",\"title\":\"Cheesy Baked Pasta with Eggplant and Artichokes\"}" 
     } 
    ] 
} 

我想將它解組爲一個結構數組,每個結構將包含3頓早餐,午餐,晚餐。所以,我想我的結構看起來像這樣,在ID是從JSON的value.id領域,Name是從JSON的value.title場,Breakfastslot:1項目,午餐是slot:2Dinner的產品與項目slot:3

type Day struct { 
    Breakfast meal 
    Lunch meal 
    Dinner meal 
} 

type meal struct { 
    ID int 
    Name string 
} 

如何在Go中完成此操作?我最初的想法是創建一箇中間的struct,其中包含來自JSON的所有數據,然後通過僅使用我需要的字段創建另一個struct。我怎樣才能做到這一點,而不使用中介struct

+0

什麼您的主要結構實現自己的執行'UnmarshalJSON'的? – mayo

+0

你是什麼意思?我對Go不是很熟悉。 –

+1

你可以編寫如何閱讀和解析一個JSON文件到一個結構中......檢查這篇文章:http://gregtrowbridge.com/golang-json-serialization-with-interfaces/ – mayo

回答

0

難道梅奧提出並實施UnmarshalJSON

func (wp *WeekPlan) UnmarshalJSON(b []byte) error { 
    wp.Days = make([]Day, 7) 
    var f map[string]*json.RawMessage 
    json.Unmarshal(b, &f) 

    var v []map[string]interface{} 
    json.Unmarshal(*f["items"], &v) 

    for _, item := range v { 

     day := int(item["day"].(float64)) - 1 
     mealnumber := int(item["slot"].(float64)) 

     var value map[string]interface{} 
     json.Unmarshal([]byte(item["value"].(string)), &value) 

     fmt.Println(value) 

     id := int(value["id"].(float64)) 
     name := value["title"].(string) 

     thisMeal := Meal{ID: id, Name: name} 

     var dateUpdate Day 

     dateUpdate = wp.Days[day] 

     switch mealnumber { 
     case 1: 
      dateUpdate.Breakfast = thisMeal 
     case 2: 
      dateUpdate.Lunch = thisMeal 
     default: 
      dateUpdate.Dinner = thisMeal 
     } 

     wp.Days[day] = dateUpdate 
    } 

    return nil 
} 


type WeekPlan struct { 
    Days []Day 
} 


type Day struct { 
    Breakfast Meal 
    Lunch  Meal 
    Dinner Meal 
} 


type Meal struct { 
    ID  int 
    Name  string 
    CookTime int 
    Image string 
}