问题描述
我正在尝试使用软件包https://godoc.org/github.com/emersion/go-jsonld来解组JSON-LD
static func commonFunction(currentController:UIViewController,dictionary: Dictionary<String,Any>) {
currentController.getDownloadURL(email: String.getString(currentController.userDetails[JsonKeys.Kemail]),orgId: String.getString(currentController.userDetails[JsonKeys.KorganizationId]),fileId: Int.getInt(dictionary[JsonKeys.KtypeId]))
}
package main
import (
"fmt"
jsonld "github.com/emersion/go-jsonld"
)
func main() {
text := `{"@context": ["http://schema.org",{ "image": { "@id": "schema:image","@type": "@id"} }],"id": "http://www.wikidata.org/entity/Q76","type": "Person","name": "Barack Obama","givenname": "Barack","familyName": "Obama","jobTitle": "44th President of the United States","image": "https://commons.wikimedia.org/wiki/File:President_Barack_Obama.jpg"}`
textBytes := []byte(text)
var container interface{}
err := jsonld.Unmarshal(textBytes,container)
fmt.Println("Error while unmarshalling json-ld: ",err.Error())
fmt.Println("Output: ",container)
}
我还检查了其他功能是否在与Error while unmarshalling json-ld: jsonld: fetching remote contexts is disabled
Output: <nil>
相同的程序包中进行编组,但没有帮助。
解决方法
您在输入中有一个远程上下文,因此您需要按照以下方式获取它:
package main
import (
"bytes"
"fmt"
jsonld "github.com/emersion/go-jsonld"
)
type person struct {
ID string `jsonld:"@id"`
Name string `jsonld:"name"`
URL *jsonld.Resource `jsonld:"url"`
Image *jsonld.Resource `jsonld:"image"`
}
func main() {
text := `{"@context": ["http://schema.org",{ "image": { "@id": "schema:image","@type": "@id"} }],"id": "http://www.wikidata.org/entity/Q76","type": "Person","name": "Barack Obama","givenName": "Barack","familyName": "Obama","jobTitle": "44th President of the United States","image": "https://commons.wikimedia.org/wiki/File:President_Barack_Obama.jpg"}`
textBytes := []byte(text)
var container person
dec := jsonld.NewDecoder(bytes.NewReader(textBytes))
dec.FetchContext = func(url string) (*jsonld.Context,error) {
var fetchedContext jsonld.Context //TODO fetch the context
return &fetchedContext,nil
}
err := dec.Decode(&container)
fmt.Println("Error while unmarshalling json-ld: ",err)
fmt.Println("Output: ",container)
}
或在输入中提供架构。您可以参考tests作为输入和输出的示例。