我如何在C#中将JSON反序列化为数组,我想创建具有JSON属性的图像。 我当前的 JSON文件看起来像这样...
{
"test":[
{
"url":"150.png","width":"300","height":"300"
},{
"url":"150.png","height":"300"
}
]
}
我的Form1有一个Picture1 我试图用Newtonsoft.json反序列化,但是我不知道如何将JSON对象反序列化为数组。 (我是开发的初学者)
这是我当前的Form1代码
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System.IO;
using System.Linq;
using System.Drawing;
namespace viewer_mvc
{
public partial class Index : Form
{
string url;
int width,height;
public Index()
{
InitializeComponent();
}
private void Index_Load(object sender,EventArgs e)
{
/*using (StreamReader file = File.OpenText("ultra_hardcore_secret_file.json"))
{
JsonSerializer serializer = new JsonSerializer();
Elements img = (Elements)serializer.Deserialize(file,typeof(Elements));
url = "../../Resources/" + img.url;
width = img.width;
height = img.height;
}
pictureBox1.Image = Image.FromFile(url);
pictureBox1.Size = new Size(width,height);*/
var json = File.ReadAllText("ultra_hardcore_secret_file.json");
RootObject obj = JsonConvert.DeserializeObject<RootObject>(json);
button1.Text = obj.test.ToString();
}
public class Elements
{
public string url { get; set; }
public string width { get; set; }
public string height { get; set; }
}
public class RootObject
{
public List<Elements> test { get; set; }
}
}
}