drl47263 2014-06-18 19:41
浏览 581
已采纳

Golang解码/解组JSON中的无效unicode

I am fetching JSON files in go that are not formatted homogeneously. For Example, I can have the following:

{"email": "\"blah.blah@blah.com\""}
{"email": "robert@gmail.com"}
{"name": "m\303\203ead"}

We can see that there will be a problem with the escaping character. Using json.Decode:

With:

{"name": "m\303\203ead"}

I get the error: invalid character '3' in string escape code

I have tried several approaches to normalise my data for example by passing by a string array (it works but there is too many edge cases), or even to filter escape characters.

Finally, I came through this article: (http://blog.golang.org/normalization) And the solution they proposed seemed very interesting.

I have tried the following

isMn := func(r rune) bool {
    return unicode.Is(unicode.Mn, r)
}

t := transform.Chain(norm.NFC, transform.RemoveFunc(isMn), norm.NFD)

fileReader, err := bucket.GetReader(filename)

transformReader := transform.NewReader(fileReader, t)

decoder := json.NewDecoder(tReader)

for {
    var dataModel Model
    if err := decoder.Decode(&kmData); err == io.EOF {
        break
    } else {
      // DO SOMETHING
    }
}

With Model being:

type Model struct {
    Name  string `json:"name" bson:"name"`
    Email string `json:"email" bson:"email"` 
}

I have tried several variations of it, but haven't been able to have it working.

So my question is how to easily handle decoding/unmarshaling JSON data with different encodings? Knowing, that I have no control on those JSON files.

If you are reading this, thank you anyway.

  • 写回答

1条回答 默认 最新

  • douhu2898 2014-06-18 21:40
    关注

    You can use json.RawMessage instead of string, that way json.Decode won't try to decode the invalid characters.

    playground : http://play.golang.org/p/fB-38KGAO0

    type Model struct {
        N  json.RawMessage `json:"name" bson:"name"`
    }
    
    func (m *Model) Name() string {
        return string(m.N)
    }
    func main() {
        s := "{\"name\": \"m\303\203ead\"}"
        r := strings.NewReader(s)
        d := json.NewDecoder(r)
        m := Model{}
    
        fmt.Println(d.Decode(&m))
        fmt.Println(m.Name())
    }
    

    Edit: Well, you can use regex, not sure how viable that is for you http://play.golang.org/p/VYJKTKmiYm:

    func cleanUp(s string) string {
        re := regexp.MustCompile(`\b(\\\d\d\d)`)
        return re.ReplaceAllStringFunc(s, func(s string) string {
            return `\u0` + s[1:]
        })
    }
    func main() {
        s := "{\"name\": \"m\303\203ead\"}"
        s = cleanUp(s)
        r := strings.NewReader(s)
        d := json.NewDecoder(r)
        m := Model{}
        fmt.Println(d.Decode(&m))
        fmt.Println(m.Name())
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀