Gorethink插入问题

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

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中。

英文:
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)

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

package main

import "fmt"

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

    fmt.Printf("%#v\n", data)
    //result, err := r.Table("order_json").Insert(data).RunWrite(session)
}
英文:

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

import "fmt"

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

	fmt.Printf("%#v\n", data)
	//result, err := r.Table("order_json").Insert(data).RunWrite(session)
}

答案2

得分: 0

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

"json_received": [
    {
        "ezpOrderId": "ezp_123",
        "firstName": "Vasanth",
        "lastName": "K",
        "orderDesc": "Sample"
    }
], // <--
英文:

you are missing comma after json_received array

&quot;json_received&quot;: [
        {
        &quot;ezpOrderId&quot;:  &quot;ezp_123&quot;,
        &quot;firstName&quot;:  &quot;Vasanth&quot;,
        &quot;lastName&quot;:  &quot;K&quot;,
        &quot;orderDesc&quot;:  &quot;Sample&quot;
        }
    ] , //&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:

确定