Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从URL使用JSON.NET反序列化JSON数组_C#_Arrays_Json_Json Deserialization - Fatal编程技术网

C# 从URL使用JSON.NET反序列化JSON数组

C# 从URL使用JSON.NET反序列化JSON数组,c#,arrays,json,json-deserialization,C#,Arrays,Json,Json Deserialization,我第一次使用json,在互联网上搜索后,我找到了json.NET。我认为它很容易使用,但我有一个问题。每次使用代码时,我都会收到一条警告: 无法将当前JSON数组(例如[1,2,3])反序列化为类型“JSON_WEB_API.Machine”,因为该类型需要一个JSON对象(例如{“name”:“value”})才能正确反序列化 这是URL中的JSON数组: [ { "id": "MachineTool 1", "guid": "not implemented",

我第一次使用json,在互联网上搜索后,我找到了json.NET。我认为它很容易使用,但我有一个问题。每次使用代码时,我都会收到一条警告:

无法将当前JSON数组(例如[1,2,3])反序列化为类型“JSON_WEB_API.Machine”,因为该类型需要一个JSON对象(例如{“name”:“value”})才能正确反序列化

这是URL中的JSON数组:

[
   {
      "id": "MachineTool 1",
      "guid": "not implemented",
      "name": "Individueller Maschinenname",
    },
    {
      "id": "MachineTool 2",
      "guid": "not implemented",
      "name": "Individueller Maschinenname",
    }
]
这是代码:

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

namespace JSON_WEB_API
{
class Program
{
    static void Main()
    {
        string json = new WebClient().DownloadString("http://localhost:12084/Machines?format=json");
        Console.WriteLine(json);

        //string json = @"[{"id":"MachineTool 1","guid":"not implemented","name":"Individueller Maschinenname"},{"id":"MachineTool 2","guid":"not implemented","name":"Individueller Maschinenname"}]
        //Console.WriteLine(json)";

        Machine machine = JsonConvert.DeserializeObject<Machine>(json);
        Console.WriteLine(machine.id);

        Console.Read();
    }
}
[DataContract]
class Machine
{
    [DataMember]
    internal string id { get; set; }

    [DataMember]
    internal string guid { get; set; }

    [DataMember]
    internal string name { get; set; }
}

}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
Net系统;
使用System.Runtime.Serialization;
使用Newtonsoft.Json;
名称空间JSON_WEB_API
{
班级计划
{
静态void Main()
{
string json=new WebClient()。下载字符串(“http://localhost:12084/Machines?format=json");
Console.WriteLine(json);
//字符串json=@“[{“id”:“MachineTool 1”,“guid”:“未实现”,“名称”:“Individualler Maschinenname”},{“id”:“MachineTool 2”,“guid”:“未实现”,“名称”:“Individualler Maschinenname”}]
//Console.WriteLine(json)”;
Machine=JsonConvert.DeserializeObject(json);
控制台写入线(机器id);
Console.Read();
}
}
[数据合同]
类机器
{
[数据成员]
内部字符串id{get;set;}
[数据成员]
内部字符串guid{get;set;}
[数据成员]
内部字符串名称{get;set;}
}
}

将其转换为机器列表

var machine = JsonConvert.DeserializeObject<List<Machine>>(json);
查看您的JSON:

[
   {
      "id": "MachineTool 1",
      "guid": "not implemented",
      "name": "Individueller Maschinenname",
    },
    {
      "id": "MachineTool 2",
      "guid": "not implemented",
      "name": "Individueller Maschinenname",
    }
]
这是一个JSON数组

查看您的JSON转换代码:

Machine machine = JsonConvert.DeserializeObject<Machine>(json);
Machine=JsonConvert.DeserializeObject(json);
您正在机器对象中转换它

但您有json数组作为响应。 您需要将JSON转换代码更改为以下内容:

要在数组中转换,请使用以下代码:

Machine[] machines =  JsonConvert.DeserializeObject.Deserialize<Machine[]>(json);
Machine[]machines=JsonConvert.DeserializeObject.Deserialize(json);
要在列表中转换,请使用以下代码:

List<Machine> machines = JsonConvert.DeserializeObject<List<Machine>>(json);
List machines=JsonConvert.DeserializeObject(json);

[DataContract]
class Machine
{
    [DataMember]
    [JsonProperty("id ")]
    internal string id { get; set; }

    [DataMember]
    [JsonProperty("guid ")]
    internal string guid { get; set; }

    [DataMember]
    [JsonProperty("name")]
    internal string name { get; set; }
}


public class MachineJson
{
    [JsonProperty("machine")]
    public Machine Machine{ get; set; }
}

var machine = JsonConvert.DeserializeObject<List<MachineJson>>(json);
[DataContract]
类机器
{
[数据成员]
[JsonProperty(“id”)]
内部字符串id{get;set;}
[数据成员]
[JsonProperty(“guid”)]
内部字符串guid{get;set;}
[数据成员]
[JsonProperty(“名称”)]
内部字符串名称{get;set;}
}
公共类机器
{
[JsonProperty(“机器”)]
公共机器{get;set;}
}
var machine=JsonConvert.DeserializeObject(json);

Try
List machines=JsonConvert.DeserializeObject(json)。您可以稍后迭代列表以访问单个
机器
对象。在MachineJson类中,您为什么编写“Machine Machine{get;set;}”不是错误的吗?如果使用列表,如何访问列表以获取其不同部分?您可以使用ForEach循环。类似这样:
foreach(机器中的var机器){Console.WriteLine(machine.id);}
但是我不能像在类中那样访问信息
[DataContract]
class Machine
{
    [DataMember]
    [JsonProperty("id ")]
    internal string id { get; set; }

    [DataMember]
    [JsonProperty("guid ")]
    internal string guid { get; set; }

    [DataMember]
    [JsonProperty("name")]
    internal string name { get; set; }
}


public class MachineJson
{
    [JsonProperty("machine")]
    public Machine Machine{ get; set; }
}

var machine = JsonConvert.DeserializeObject<List<MachineJson>>(json);