无法将json数据解组到go中的结构(无法将数组解组到Go struct字段中)

时间:2019-12-11 09:03:55

标签: go

我正在使用API​​来接收某个组织的所有招聘广告,我收到的JSON数据非常大,我想在Go中使用此数据,但是我遇到了一些问题,需要整理成结构才能使用进一步。这可能是一个非常简单的解决方案,对我来说是盲目的,因为我这个问题引起了一些头痛。代码中的API密钥是公共的,因此与Stackoverflow共享它没有问题。

代码:

public partial class Form1 : Form
{
    private DataGridViewTextBoxColumn iView;
    private DataGridViewTextBoxColumn kView;
    private DataGridViewTextBoxColumn cView;

    public Form1()
    {
        InitializeComponent();

    }

    private void textBox1_TextChanged(object sender, EventArgs e)

    {

         BindingSource bs = new BindingSource();
         bs.DataSource = dataGridView1.DataSource;

         bs.Filter = "indexView like '%" + textBox1.Text + "%'";
         bs.Filter = "kategorieView like '%" + textBox1.Text + "%'";
         bs.Filter = "inhaltView like '%" + textBox1.Text + "%'";

         dataGridView1.DataSource = bs;

    }

    private void Form1_Load(object sender, EventArgs e)

    {
        indexView = iView;
        kategorieView = kView;
        inhaltView = cView;
    }

}

错误:

package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "log"
    "net/http"
)

type JsonData struct {
    Content JsonContent `json:"content"`
    TotalElements int `json:"totalElements"`
    PageNumber int `json:"pageNumber"`
    PageSize int `json:"pageSize"`
    TotalPages int `json:"totalPages"`
    First bool `json:"first"`
    Last bool `json:"last"`
    Sort string `json:"sort"`
}
type JsonContent struct {
    Uuid string `json:"uuid"`
    Published string `json:"published"`
    Expires string `json:"expires"`
    Updated string `json:"updated"`
    WorkLoc WorkLocations `json:"workLocations"`
    Title string `json:"title"`
    Description string `json:"description"`
    SourceUrl string `json:"sourceurl"`
    Source string `json:"source"`
    ApplicationDue string `json:"applicationDue"`
    OccupationCat OccupationCategories `json:"occupationCategories"`
    JobTitle string `json:"jobtitle"`
    Link string `json:"link"`
    Employ Employer `json:"employer"`
    EngagementType string `json:"engagementtype"`
    Extent string `json:"extent"`
    StartTime string `json:"starttime"`
    PositionCount interface{} `json:"positioncount"`
    Sector string `json:"sector"`
}
type WorkLocations struct {
    Country string `json:"country"`
    Address string `json:"address"`
    City string `json:"city"`
    PostalCode string `json:"postalCode"`
    County string `json:"county"`
    Municipal string `json:"municipal"`
}
type OccupationCategories struct {
    Level1 string `json:"level1"`
    Level2 string `json:"level2"`
}
type Employer struct {
    Name string `json:"name"`
    Orgnr string `json:"orgnr"`
    Description string `json:"description"`
    Homepage interface{} `json:"homepage"`
}
func main() {
    var datas JsonData
    url := "https://arbeidsplassen.nav.no/public-feed/api/v1/ads?page=1&size=5000&published=%2A&county=Oslo"

    // Create a Bearer string by appending string access token
    var bearer = "Bearer " + "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJwdWJsaWMudG9rZW4udjFAbmF2Lm5vIiwiYXVkIjoiZmVlZC1hcGktdjEiLCJpc3MiOiJuYXYubm8iLCJpYXQiOjE1NTc0NzM0MjJ9.jNGlLUF9HxoHo5JrQNMkweLj_91bgk97ZebLdfx3_UQ"

    // Create a new request using http
    req, err := http.NewRequest("GET", url, nil)

    // add authorization header to the req
    req.Header.Add("Authorization", bearer)

    // Send req using http Client
    client := &http.Client{}

    resp, err := client.Do(req)
    if err != nil {
        log.Fatal(err)
    }

    body, _ := ioutil.ReadAll(resp.Body)
    fmt.Println(string(body))

    err = json.Unmarshal(body, &datas)
    if err != nil {
        log.Fatal(err)
    }
    // fmt.Println(datas.TotalPages)
}

一个'uuid'的示例:

2019/12/11 09:52:35 json: cannot unmarshal array into Go struct field JsonData.content of type main.JsonContent

1 个答案:

答案 0 :(得分:0)

两个可爱的人“ zerkms”,“ tclass”找到了解决方案。

您声称Content JsonContent json:"content"是一个JsonContent,但它是它们的数组,因此[] JsonContent

如您在示例json中看到的那样,

内容字段实际上是一个数组。在您的执行结构中不是。您必须将结构更改为Content [] JsonContent json:“ content”

非常感谢你们!