使用Golang+Elastic Search向jQuery传JSON时解析报错:Unexpected token { in JSON
解决jQuery解析JSON时的"Unexpected token { in JSON"错误
嘿,这问题我太熟了!你碰到的"Unexpected token { in JSON "错误,根源是你从Golang返回的JSON格式完全不符合规范——你把两个独立的JSON对象直接拼在一起了,这根本不是有效的JSON结构,解析器当然会报错。
先看看你当前返回的内容:
{"id":212,"user_id":10,"meta_description":"Plot,G-16, Islamabad,In G-16, Islamabad, Islamabad","property_type":"16","Location1":"Pakistan","Location2":"Islamabad","Location3":null,"Location4":"Islamabad","price":1850000,"bedrooms":0,"bathrooms":0,"add_date":{"Time":"0001-01-01T00:00:00Z","Valid":false}} {"id":213,"user_id":10,"meta_description":"Plot,G-16, Islamabad,In G-16, Islamabad, Islamabad","property_type":"16","Location1":"Pakistan","Location2":"Islamabad","Location3":null,"Location4":"Islamabad","price":1800000,"bedrooms":0,"bathrooms":0,"add_date":{"Time":"0001-01-01T00:00:00Z","Valid":false}}
看到没?两个{...}对象直接连在一起,没有用数组[]包裹,也没有逗号分隔——这在JSON里是绝对不允许的,解析器读到第一个对象结束的}之后,又看到第二个对象的{,就会抛出"Unexpected token {"的错误。
解决方案:把多个JSON对象包裹成数组返回
在Golang里,你需要先把从Elasticsearch获取到的所有文档收集到一个切片(slice)里,然后一次性将整个切片编码成JSON数组,而不是逐个编码单个对象。
举个具体的代码例子:
// 先定义对应ES文档的结构体(根据你的实际字段调整) type Property struct { ID int `json:"id"` UserID int `json:"user_id"` MetaDescription string `json:"meta_description"` PropertyType string `json:"property_type"` Location1 string `json:"Location1"` Location2 string `json:"Location2"` Location3 *string `json:"Location3"` // 用指针类型处理null值 Location4 string `json:"Location4"` Price int `json:"price"` Bedrooms int `json:"bedrooms"` Bathrooms int `json:"bathrooms"` AddDate struct { Time time.Time `json:"Time"` Valid bool `json:"Valid"` } `json:"add_date"` } // 在你的HTTP处理函数里: func getPropertiesHandler(w http.ResponseWriter, r *http.Request) { // 1. 从Elasticsearch查询数据(这里替换成你的实际查询逻辑) searchResults, err := esClient.Search(...) if err != nil { http.Error(w, "Failed to fetch data from Elasticsearch", http.StatusInternalServerError) return } // 2. 把所有查询到的文档收集到切片中 var properties []Property for _, hit := range searchResults.Hits.Hits { var prop Property if err := json.Unmarshal(hit.Source, &prop); err != nil { http.Error(w, "Failed to parse document", http.StatusInternalServerError) return } properties = append(properties, prop) } // 3. 设置正确的Content-Type头,然后一次性编码整个切片为JSON数组 w.Header().Set("Content-Type", "application/json") if err := json.NewEncoder(w).Encode(properties); err != nil { http.Error(w, "Failed to encode JSON", http.StatusInternalServerError) return } }
这样修改后,你返回的JSON会变成标准的数组格式:
[ {"id":212,"user_id":10,"meta_description":"Plot,G-16, Islamabad,In G-16, Islamabad, Islamabad","property_type":"16","Location1":"Pakistan","Location2":"Islamabad","Location3":null,"Location4":"Islamabad","price":1850000,"bedrooms":0,"bathrooms":0,"add_date":{"Time":"0001-01-01T00:00:00Z","Valid":false}}, {"id":213,"user_id":10,"meta_description":"Plot,G-16, Islamabad,In G-16, Islamabad, Islamabad","property_type":"16","Location1":"Pakistan","Location2":"Islamabad","Location3":null,"Location4":"Islamabad","price":1800000,"bedrooms":0,"bathrooms":0,"add_date":{"Time":"0001-01-01T00:00:00Z","Valid":false}} ]
这个格式完全符合JSON规范,jQuery的$.getJSON()或者JSON.parse()都能正常解析,不会再报错啦!
内容的提问来源于stack exchange,提问作者Sarmad Nazir
相关产品推荐
相关产品推荐

