首页 / JSON / JSON列表转换器C#
JSON列表转换器C#
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了JSON列表转换器C#,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4246字,纯文字阅读大概需要7分钟。
内容图文
![JSON列表转换器C#](/upload/InfoBanner/zyjiaocheng/797/9efa5a78ce5b42faa7778578dc65efb1.jpg)
我完全是c#初学者.我试图在c#中转换json数据.我正在努力争取这份名单.只要我能够转换基本方法,我就会在列表中出现错误.你介意给我一些建议如何解决我的问题吗?
原始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.
谢谢大家.
解决方法:
您的代码存在一些问题.
>由于JSON被[…]包围,这意味着它是一个或多个数组,因此您需要使用List(或类似)结构.在您的情况下,您可以完全删除Rootobject类并直接反序列化为List< Class1>.例如:
var result = JsonConvert.DeserializeObject<List<Class1>>(json);
> Data1类使用日期作为属性名称.我建议完全删除该类并修改thre 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}");
内容总结
以上是互联网集市为您收集整理的JSON列表转换器C#全部内容,希望文章能够帮你解决JSON列表转换器C#所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。