douxiong5972 2018-11-07 06:21
浏览 28
已采纳

Go中的JSON解析-技巧[重复]

This question already has an answer here:

How to parse this json using Go?

 timelinedata := '{
   "2016-08-17T00:00:00.000Z": 4,
   "2016-11-02T00:00:00.000Z": 1,
   "2017-08-30T00:00:00.000Z": 1
   } '

I want the dates and the values in separate variables by looping over the json. Currently I am doing it in this way

var timeline map[string]int

json.Unmarshal([]byte(timelinedata),

for k, v := range timeline {
            new_key := k
            new_val := v
            println("val--->>", new_key, new_val)
        }

The problem is that the output is not in proper order as like the json input is. Every time I run the loop the output order varies. I want to map the json in exact order as like the input. I think I am not maping it in a proper way---

</div>
  • 写回答

1条回答 默认 最新

  • dongweihuai5601 2018-11-07 07:41
    关注

    You should not assume that the key order in a JSON object means anything:

    From the introduction of RFC 7159 (emphasis mine):

    An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array.

    An array is an ordered sequence of zero or more values.

    In addition to that, you shouldn't assume that the producer of the JSON document has been in control of the key/value order; maps are unordered in most languages, so it mostly comes down to the used encoding library. If the producer did care about the order, they would use an array.

    That being said, if you really are interested in the order of the JSON keys, you have to decode the object piece by piece, using json.Decoder.Token:

    package main
    
    import (
        "encoding/json"
        "fmt"
        "log"
        "strings"
    )
    
    func main() {
        j := `{
            "2016-08-17T00:00:00.000Z": 4,
            "2016-11-02T00:00:00.000Z": 1,
            "2017-08-30T00:00:00.000Z": 1
        }`
    
        dec := json.NewDecoder(strings.NewReader(j))
        for dec.More() {
            t, err := dec.Token()
            if err != nil {
                log.Fatal(err)
            }
    
            switch t := t.(type) {
            case json.Delim:
                // no-op
            case string:
                fmt.Printf("%s => ", t)
            case float64:
                fmt.Printf("%.0f
    ", t)
            case json.Number:
                fmt.Printf(" %s
    ", t)
            default:
                log.Fatalf("Unexpected type: %T", t)
            }
        }
    }
    

    Try it on the Playground: https://play.golang.org/p/qfXcOfOvKws

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示