duanhan7001 2013-02-05 14:22
浏览 44
已采纳

如何在Go中解析此JSON字符串?

The JSON-string in question looks like this:

{
"development":{
    "connector":[
         {"id":"connector-server-1", "host":"127.0.0.1", "port":4050, "wsPort":3050},
         {"id":"connector-server-2", "host":"127.0.0.1", "port":4051, "wsPort":3051},
         {"id":"connector-server-3", "host":"127.0.0.1", "port":4052, "wsPort":3052}
     ],
    "chat":[
         {"id":"chat-server-1", "host":"127.0.0.1", "port":6050},
         {"id":"chat-server-2", "host":"127.0.0.1", "port":6051},
         {"id":"chat-server-3", "host":"127.0.0.1", "port":6052}
    ],
    "gate":[
     {"id": "gate-server-1", "host": "127.0.0.1", "wsPort": 3014}
]
},
"production":{
   "connector":[
         {"id":"connector-server-1", "host":"127.0.0.1", "port":4050, "wsPort":3050},
         {"id":"connector-server-2", "host":"127.0.0.1", "port":4051, "wsPort":3051},
         {"id":"connector-server-3", "host":"127.0.0.1", "port":4052, "wsPort":3052}
     ],
    "chat":[
         {"id":"chat-server-1", "host":"127.0.0.1", "port":6050},
         {"id":"chat-server-2", "host":"127.0.0.1", "port":6051},
         {"id":"chat-server-3", "host":"127.0.0.1", "port":6052}
    ],
    "gate":[
     {"id": "gate-server-1", "host": "127.0.0.1", "wsPort": 3014}
]
}
}

and I want to parse it with code like this:

package config

import(
    "sync"
    "io/ioutil"
    "encoding/json"
    "errors"
    "log"
)

type Service struct {
    Id string `json:"id"`
    Host string `json:"host"`
    Port uint `json:"port"`
    QueryPort uint `json:"queryPort"`
    WsPort uint `json:"wsPort"`
    ServiceType string 
}

type Config struct {
    Services []Service
    Master Service
    Mutex sync.RWMutex
}

func LoadServers(filepath, env string) (*Config, error) {
    // 读取文件
    content, err := ioutil.ReadFile(filepath)
    if err != nil {
        return nil, err
    }

    configs := make(map[string]map[string][]Service, 0)
    err = json.Unmarshal(content, configs)
    if err != nil {
        return nil, err
    }
}

I expect my code to parse this JSON-string to a map[string]map[string][]Service.

but It shows the error:

json: Unmarshal(non-pointer map[string]map[string][]config.Service)
  • 写回答

2条回答 默认 最新

  • du5910 2013-02-05 20:40
    关注

    To build on @peterSO's answer, if you are up for something fancy, the encoding/json package has the Decoder type, which allows you to decode JSON directly from an io.Reader, which is an interface that os.File satisfies.

    This would allow you to use the os package, rather than io/ioutil, which could save you an import (seeing as ioutil already imports os.)

    package main
    
    import (
        "fmt"
        "encoding/json"
        "os"
    )
    
    func main() {
        pathToFile := "jsondata.txt"
    
        file, err := os.OpenFile(pathToFile, os.O_RDONLY, 0644)
        if err != nil {
            fmt.Println(err)
            os.Exit(1)
        }
    
        configs := make(map[string]map[string][]Service, 0)
        err = json.NewDecoder(file).Decode(&configs)
        if err != nil {
            fmt.Println(err)
            os.Exit(1)
        }
    }
    

    This way, you can decode JSON directly from a file or data stream. It's probably unnecessary if you're doing something simple and want to avoid this sort of thing, but nonetheless something to be aware of.

    Good luck!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗