gpt4 book ai didi

c# - JSON 列表转换器 C#

转载 作者:太空狗 更新时间:2023-10-30 01:13:11 24 4
gpt4 key购买 nike

我完全是c#初学者。我正在尝试在 c# 中转换 json 数据。我正在为这份 list 苦苦挣扎。只要我能够转换基本方法,我就会在列表中出现错误。您介意给我一些解决问题的建议吗?

原始 JSON 数据

[{"data": {"Temperature": {"data": {"2018-07-04 13:05:00": 20.9224991798401}, "meta": {"units": "Celsius", "name": "Temperature", "theme": "Weather"}}}, "latest": "2018-07-04 13:05:00", "sensor_height": -999, "type": "Weather", "base_height": -999, "geom": {"coordinates": [-1.62469, 54.98274], "type": "Point"}, "active": "True", "name": "sportshall_oat", "source": {"document": null, "fancy_name": "BMS", "db_name": "Bms", "third_party": false, "web_display_name": "BMS"}}]

主类

using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
var client = new WebClient();

var text = client.DownloadString("http://uoweb1.ncl.ac.uk/api/v1/sensor/live.json?sensor_name=sportshall_oat&api_key=4dopcdjiu3wtzfl32hn94hbf5ubm3q89jbh18iaxaqdzc10nlgbebqqvxqyt3ymydi59fjnyrmuqtgtdxb1sm5msac");

Rootobject ro = JsonConvert.DeserializeObject<Rootobject>(text);
Console.WriteLine("current time = " + ro.Property1);

Class1 c1 = JsonConvert.DeserializeObject<Class1>(text);

Data data = JsonConvert.DeserializeObject<Data>(text);

Temperature temperature = JsonConvert.DeserializeObject<Temperature>(text);
Console.WriteLine("data = " + temperature.data);

Data1 d1 = JsonConvert.DeserializeObject<Data1>(text);

Meta meta = JsonConvert.DeserializeObject<Meta>(text);

Geom geom = JsonConvert.DeserializeObject<Geom>(text);

Source source = JsonConvert.DeserializeObject<Source>(text);

}
}
}

JSON 数据(由 Visual Studio 生成)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{

public class Rootobject
{
public Class1[] Property1 { get; set; }
}

public class Class1
{
public Data data { get; set; }
public string latest { get; set; }
public int sensor_height { get; set; }
public string type { get; set; }
public int base_height { get; set; }
public Geom geom { get; set; }
public string active { get; set; }
public string name { get; set; }
public Source source { get; set; }
}

public class Data
{
public Temperature Temperature { get; set; }
}

public class Temperature
{
public Data1 data { get; set; }
public Meta meta { get; set; }
}

public class Data1
{
public float _20180704130500 { get; set; }
}

public class Meta
{
public string units { get; set; }
public string name { get; set; }
public string theme { get; set; }
}

public class Geom
{
public float[] coordinates { get; set; }
public string type { get; set; }
}

public class Source
{
public object document { get; set; }
public string fancy_name { get; set; }
public string db_name { get; set; }
public bool third_party { get; set; }
public string web_display_name { get; set; }
}

}

当我尝试运行我的程序时,控制台被压碎了。调试后我知道问题出在哪里,但我不知道如何解决。有错误信息

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'ConsoleApp2.Rootobject' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.

提前谢谢大家。

最佳答案

您的代码存在一些问题。

  1. 由于 JSON 被 [...] 包围,这意味着它是一个数组或项目,因此您需要使用 List (或类似的)结构。在您的情况下,您可以删除 Rootobject完全分类并直接反序列化为 List<Class1> .例如:

    var result = JsonConvert.DeserializeObject<List<Class1>>(json);
  2. Data1类使用日期作为属性名称。我建议完全删除该类并修改三个 Temperature类使用 Dictonary<DateTime, float> :

    public class Temperature
    {
    public Dictionary<DateTime, float> data { get; set; }
    public Meta meta { get; set; }
    }

    现在您可以应对任何DateTime被传入,你会像这样使用它:

    var data = obj[0].data.Temperature.data;
    Console.WriteLine($"The temperature on {data.Key} was {data.Value}");

关于c# - JSON 列表转换器 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51176424/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com