dsq1982 2014-04-01 18:27
浏览 49
已采纳

在Go中解析JSON(无需解组)

I need to parse arbitrary JSON structures in Golang for the sake of translating them into another language format (whether it be C structs or XML), but the Golang library apparently makes this impossible to do with Marshalling and Unmarshalling into structs and maps.

I don't necessarily even need a map-like data structure from the JSON input anyway. All I need is a recursive parser, maybe even something like PHP's XMLParser where you decide what do do yourself at each node, so I can translate it without the need for maps or interfaces.

  • 写回答

1条回答 默认 最新

  • dongyuan8024 2014-04-01 19:10
    关注

    There is a scanner package in the megajson package that allows you to walk through the json yourself.

    scanner := scanner.NewScanner(strings.NewReader(`{"foo":"bar", 
    "bat":1293,"truex":true,"falsex":false,"nullx":null,"nested":{"xxx":"yyy"}}`))
    
    // Scan for the next JSON token.
    position, token, err := scanner.Scan()
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?