博客合辑 诗词鉴赏

针对从服务端获取数据时候JSON串的key 没有双引号导致不能解析问题

 
0 评论0 浏览
示例JSON:
{
	total: 100,
	rows: [{
		goodId: "4893ed8518ff41268baf5a2f7f3e5398",
		reportedStaffid: "",
		reportedName: "",
		reportedPst: "",
		reportedOrg: "",
		reportedTime: "",
		goodTitle: "",
		goodContent: "",
		reportStaffid: "",
		reportName: "",
		reportPst: "",
		reportOrg: "",
		publishRange: "",
		praiseCnt: "",
		commentCnt: "0",
	}]
}
解决方法:

方法1:


js = strings.Replace(js,`{`,`{"`,-1)
	js = strings.Replace(js ,`:`,`":`,-1)
	js = strings.Replace(js,`,`,`,"`,-1)
	js = strings.Replace(js,`,"{`,`,{`,-1)
	fmt.Println(js)
	unmarshal := make(map[string]interface{})
	err := json.Unmarshal([]byte(js), &unmarshal)
	if err != nil {
		panic(err)
	}
	fmt.Println(unmarshal)

方法2:

reg:=regexp.MustCompile(`[\w]+[:]`)
reg.ReplaceAllStringFunc(resStr,func(s string) string{
     reg2:=regexp.MustCompile(`[\w]+`)
     s=reg2.FindAllString(s,-1)[0]
     return `"`+s+`":`
})