在不知道Golang中结构的情况下,在JSON文件中编辑值[重复]

I have a JSON file which has data like below

{
    "id": "0001",
    "type": "donut",
    "name": "Cake",
    "ppu": 0.55,
    "batters": {
        "batter": [{
            "id": "1001",
            "type": "Regular"
        }, {
            "id": "1002",
            "type": "Chocolate"
        }]
    },
    "topping": [{
        "id": "5001",
        "type": "None"
    }, {
        "id": "5002",
        "type": "Glazed"
    }, {
        "id": "5005",
        "type": "Sugar"
    }, {
        "id": "5007",
        "type": "Powdered Sugar"
    }]
}

I need to edit 'id' values which are in 'batter' array. Assume I didn't use ant struct type to unmarshall. After editing I need to write updated JSON string back to file again.

</div>

You can do it without a struct, but this means using casts all over the place. Here is a working example:

package main

import (
    "encoding/json"
    "fmt"
)

func main() {
    str := "{ \"id\": \"0001\", \"type\": \"donut\", \"name\": \"Cake\", \"ppu\": 0.55, \"batters\": { " +
                "\"batter\": [{ \"id\": \"1001\", \"type\": \"Regular\" }, { \"id\": \"1002\", \"type\": " +
                "\"Chocolate\" }] }, \"topping\": [{ \"id\": \"5001\", \"type\": \"None\" }, { \"id\": \"5002\", " +
                "\"type\": \"Glazed\" }, { \"id\": \"5005\", \"type\": \"Sugar\" }, { \"id\": \"5007\", \"type\": " +
                "\"Powdered Sugar\" }] }"

    jsonMap := make(map[string]interface{})
    err := json.Unmarshal([]byte(str), &jsonMap)
    if err != nil {
        panic(err)
    }

    batters := jsonMap["batters"].(map[string]interface{})["batter"]

    for _, b := range(batters.([]interface{})) {
        b.(map[string]interface{})["id"] = "other id"
    }

    str2, err := json.Marshal(jsonMap)
    fmt.Println(string(str2))   
}

I think you are better off using a struct.