问题
I want to know why I get this error when I use these statements:
string result = await client.GetStringAsync(RestUrl);
return JsonConvert.DeserializeObject<IEnumerable<Book>>(result);
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[BookClient.Data.Book]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object
this is the result string File:
"{\"TOTAL\":177,\"PRODUCTS\":[{\"CODE\":\"T55-2A\",\"PRICE\":59.9500,\"NAME\":\"Arrive In Style\"},{\"CODE\":\"F1-231\",\"PRICE\":49.9500}]}"
Thanks
回答1:
You seem to be trying to deserialize an object into an array. You need to create a class that contains an array inside. Something like this:
using System.Collections.Generic;
using Newtonsoft.Json;
using System.Diagnostics;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
const string result = "{\"TOTAL\":177,\"PRODUCTS\":[{\"CODE\":\"T55-2A\",\"PRICE\":59.9500,\"NAME\":\"Arrive In Style\"},{\"CODE\":\"F1-231\",\"PRICE\":49.9500}]}";
var deserialized = JsonConvert.DeserializeObject<Result>(result);
Debug.Assert(deserialized.Books.Count == 2);
}
}
public class Result
{
[JsonProperty("TOTAL")]
public int Total { get; set; }
[JsonProperty("PRODUCTS")]
public List<Book> Books { get; set; }
}
public class Book
{
[JsonProperty("CODE")]
public string Code { get; set; }
[JsonProperty("PRICE")]
public float Price { get; set; }
[JsonProperty("NAME")]
public string Name { get; set; }
}
}
回答2:
Your result seems to be an object and not an array. So you need to create a class to store the deserialized result, with attributes total (int) and products (array). And then you can access the array by the products property.
回答3:
The error actually is kind of confusing. The real meaning of the error is that there is no way the code knows which type to deserialize to. Consider the scenario -
public interface I
{
public int v { get; set;}
}
public class A: I
{
public int v { get; set;}
}
public class B: I
{
public int v { get; set;}
}
Now consider this code block -
var result = JsonConvert.DeserializeObject<I>("{\"v\" : 10}");
Can you tell me what will be the outcome? Interface cannot be instantiated. So it either have to be A or B? Which class will the result be? A or B? Actually no one cannot tell - so does the compiler.
The same thing is happening here. -
JsonConvert.DeserializeObject<IEnumerable<Book>>(result);
You are deserializing to IEnumerable<Book>, but IEnumerable<Book> is not a concrete implementation. This is just an interface, a signature of the result but not the resultant type itself. So the converter cannot tell which class to use. That is why it is telling you to use primitive types or something that can be created.
And from the looks of the error it is trying to use List<Book> to deserialize to, but it cannot do that either, because the JSON string provided is not an array, it is an object, hence the error-
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[BookClient.Data.Book]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly. To fix this error either change the JSON to a JSON array (e.g. [1,2,3]) or change the deserialized type so that it is a normal .NET type (e.g. not a primitive type like integer, not a collection type like an array or List) that can be deserialized from a JSON object. JsonObjectAttribute can also be added to the type to force it to deserialize from a JSON object
If you are looking for a solution then follow @Maria Ines Parnisari answer. That is a good solution to your problem.
来源:https://stackoverflow.com/questions/44192365/jsonconvert-deserializeobjectienumerablebook