英文:
go grammes - how to get property from gremlin, not just the value with go grammes
问题
你好,以下是翻译好的内容:
package main
import (
"encoding/json"
"fmt"
"log"
"github.com/northwesternmutual/grammes"
)
func main() {
client, err := grammes.DialWithWebSocket("ws://localhost:8182/gremlin")
if err != nil {
log.Fatalf("Connection error: %s", err.Error())
}
data, err := client.ExecuteQuery(g.V().Has("person", "name", "aksan").Values())
if err != nil {
log.Fatalf("Querying error: %s", err.Error())
}
var responseStruct []interface{}
err = json.Unmarshal(data[0], &responseStruct)
if err != nil {
log.Fatalf("unmarshal error: %s", err.Error())
}
result := make(map[string]interface{})
for i := 0; i < len(responseStruct); i += 2 {
field := responseStruct[i].(string)
value := responseStruct[i+1]
result[field] = value
}
PrettyPrint(result, "result")
}
func PrettyPrint(data interface{}, prefix string) {
prettyJSON, err := json.MarshalIndent(data, "", " ")
if err != nil {
log.Fatalf("JSON marshaling error: %s", err.Error())
}
fmt.Printf("%s:\n%s\n", prefix, string(prettyJSON))
}
响应结果如下:
{
"height": {
"@type": "g:Int64",
"@value": 6
},
"code": "p01",
"weight": {
"@type": "g:Int64",
"@value": 10
},
"name": "aksan",
"age": {
"@type": "g:Int32",
"@value": 29
}
}
希望对你有帮助!
英文:
hello i'm using golang package "github.com/northwesternmutual/grammes" and want to get the field not just the value from response client so i can decode it to json
data, err := client.ExecuteQuery(g.V().Has("person", "name", "aksan").Values())
if err != nil {
log.Fatalf("Querying error: %s", err.Error())
}
var responseStruct map[string]interface{}
err = json.Unmarshal(data[0], &responseStruct)
if err != nil {
log.Fatalf("unmarshal error: %s", err.Error())
}
// Log out the response.
PrettyPrint(responseStruct["@value"], "result")
the response is
[
{
"@type": "g:Int64",
"@value": 6
},
"p01",
{
"@type": "g:Int64",
"@value": 10
},
"aksan",
{
"@type": "g:Int32",
"@value": 29
}
]
what i want is the result will showing field to like @fields or anything maybe something like this
[
"height":{
"@type": "g:Int64",
"@value": 6
},
"code":"p01",
"weight":{
"@type": "g:Int64",
"@value": 10
},
"name":"aksan",
"age":{
"@type": "g:Int32",
"@value": 29
}
]
答案1
得分: 3
使用ValueMap()
代替Values()
。
英文:
Use ValueMap()
instead of Values()
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论