JSON-LD 解组出现错误 "jsonld: fetching remote contexts is disabled"

JSON-LD unmarshalling getting error "jsonld: fetching remote contexts is disabled"

我正在尝试使用包 https://godoc.org/github.com/emersion/go-jsonld

解组 JSON-LD
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)
}

Output

Error while unmarshalling json-ld:  jsonld: fetching remote contexts is disabled
Output:  <nil>

我还检查了其他函数以在同一个包中解组,例如 func UnmarshalWithContext(b []byte, v interface{}, ctx *Context) error 但没有帮助。

您在输入中有一个远程上下文,因此您需要获取它,如下所示:

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