Gorethink插入问题

huangapple go评论116阅读模式
英文:

Gorethink Insert Issue

问题

var data = map[string]interface{}{
"json_received": [
{
"ezpOrderId": "ezp_123",
"firstName": "Vasanth",
"lastName": "K",
"orderDesc": "Sample"
}
],
"created_on": "03-22-2015",
"status": "1"
}

result, err := r.Table("order_json").Insert(data).RunWrite(session)

当我尝试运行这个程序时,我得到了一个错误,错误信息是"json_received"后面缺少操作数。

请帮助我通过Go编程将数据变量插入到RethinkDB中。

英文:
  1. var data = map[string]interface{}{
  2. "json_received": [
  3. {
  4. "ezpOrderId": "ezp_123",
  5. "firstName": "Vasanth",
  6. "lastName": "K",
  7. "orderDesc": "Sample"
  8. }
  9. ]
  10. "created_on": "03-22-2015",
  11. "status": "1"
  12. }
  13. result, err := r.Table("order_json").Insert(data).RunWrite(session)

When i m tried to run this program i got the error as "missing operand" after "json_received":[ line.

Please help me to insert the data variable in rethink db via go programming..

答案1

得分: 2

Go语言不支持像你尝试做的那样的JSON字面量。

这是一个修复后的版本(在Play上查看)。

请注意,对于所有的子结构,在创建时必须声明类型。
你试图让json_received成为一个JSON对象列表,所以我使用了[]map[string]interface{}

另外,正如其他人指出的,多行的map/list字面量在每行后面必须有一个逗号,比如:orderDescstatus

  1. package main
  2. import "fmt"
  3. func main() {
  4. var data = map[string]interface{}{
  5. "json_received": []map[string]interface{}{
  6. {
  7. "ezpOrderId": "ezp_123",
  8. "firstName": "Vasanth",
  9. "lastName": "K",
  10. "orderDesc": "Sample",
  11. },
  12. },
  13. "created_on": "03-22-2015",
  14. "status": "1",
  15. }
  16. fmt.Printf("%#v\n", data)
  17. //result, err := r.Table("order_json").Insert(data).RunWrite(session)
  18. }
英文:

Go does not support json literals like you are trying to do.

Here's a fixed version (on Play).

Notice that for all sub structures, you must declare the type when creating it.
You were trying to make json_recieved to be a list of json objects, so I used []map[string]interface{}.

And, as others have pointed out, multi-line map/list literals must have a comma after each line, as in: orderDesc, status.

package main

  1. import "fmt"
  2. func main() {
  3. var data = map[string]interface{}{
  4. "json_received": []map[string]interface{}{
  5. {
  6. "ezpOrderId": "ezp_123",
  7. "firstName": "Vasanth",
  8. "lastName": "K",
  9. "orderDesc": "Sample",
  10. },
  11. },
  12. "created_on": "03-22-2015",
  13. "status": "1",
  14. }
  15. fmt.Printf("%#v\n", data)
  16. //result, err := r.Table("order_json").Insert(data).RunWrite(session)
  17. }

答案2

得分: 0

你在json_received数组后面漏掉了逗号。

  1. "json_received": [
  2. {
  3. "ezpOrderId": "ezp_123",
  4. "firstName": "Vasanth",
  5. "lastName": "K",
  6. "orderDesc": "Sample"
  7. }
  8. ], // <--
英文:

you are missing comma after json_received array

  1. &quot;json_received&quot;: [
  2. {
  3. &quot;ezpOrderId&quot;: &quot;ezp_123&quot;,
  4. &quot;firstName&quot;: &quot;Vasanth&quot;,
  5. &quot;lastName&quot;: &quot;K&quot;,
  6. &quot;orderDesc&quot;: &quot;Sample&quot;
  7. }
  8. ] , //&lt;--

huangapple
  • 本文由 发表于 2016年3月22日 17:31:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/36150650.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定