简体   繁体   中英

Deserialize JSON Array with JSON.NET from URL

I am using json for the first time and after a search in the internet I found JSON.NET. I thought it is easy to use but I have a problem with it. Every time i use the code i get a warning:

Cannot deserialize the current JSON array (eg [1,2,3]) into type 'JSON_WEB_API.Machine' because the type requires a JSON object (eg {"name":"value"}) to deserialize correctly.

This is the JSON array from the URL:

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

And this is the CODE:

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; }
}

}

convert it to list of Machine

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

To Access the Data run the foreach on machine.

foreach(var data in machine ) 
{ 
     Console.WriteLine(data.id); 
}

Look on your JSON:

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

This is a JSON Array.

and Look on your JSON Convert code:

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

You are converting it in Machine object.

But you have json array as response. You need to change your JSON convert code to something like that:

To Convert it in Array use this code:

Machine[] machines =  JsonConvert.DeserializeObject.Deserialize<Machine[]>(json);

To Convert it in List use this code:

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

Decorate ur Model With

[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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM