Cannot Deserialize JSON Array into Type - JSON.Net

Cannot deserialize JSON array into type - Json.NET

You have to write a custom JsonConverter:

    public class CountryModelConverter : JsonConverter
{

public override bool CanConvert(Type objectType)
{
if (objectType == typeof(CountryModel))
{
return true;
}

return false;
}

public override object ReadJson(JsonReader reader, Type objectType
, object existingValue, JsonSerializer serializer)
{
reader.Read(); //start array
//reader.Read(); //start object
JObject obj = (JObject)serializer.Deserialize(reader);

//{"page":1,"pages":1,"per_page":"50","total":35}
var model = new CountryModel();

model.Page = Convert.ToInt32(((JValue)obj["page"]).Value);
model.Pages = Convert.ToInt32(((JValue)obj["pages"]).Value);
model.Per_Page = Int32.Parse((string) ((JValue)obj["per_page"]).Value);
model.Total = Convert.ToInt32(((JValue)obj["total"]).Value);

reader.Read(); //end object

model.Countries = serializer.Deserialize<List<Country>>(reader);

reader.Read(); //end array

return model;
}

public override void WriteJson(JsonWriter writer, object value
, JsonSerializer serializer)
{
throw new NotImplementedException();
}
}

And tag the CountryModel with that converter (I also had to switch some int to string):

    [JsonConverter(typeof(CountryModelConverter))]
public class CountryModel
{
public int Page { get; set; }
public int Pages { get; set; }
public int Per_Page { get; set; }
public int Total { get; set; }

public List<Country> Countries { get; set; }
}

public class Country
{
public string Id { get; set; }
public string Iso2Code { get; set; }
public string Name { get; set; }
public Region Region { get; set; }
}

public class Region
{
public string Id { get; set; }
public string Value { get; set; }
}

Then you should be able to deserialize like this:

var output = JsonConvert.DeserializeObject<CountryModel>(result);

Cannot deserialize the current JSON object into type because the type requires a JSON array

you have several bugs in your code.Replace List<clsRaceTable> , List<clsCircuit> , List<clsLocation> with clsRaceTable,clsCircuit and clsLocation. Your classes should be

public class Root
{
public clsMRData MRData { get; set; }
}
public class clsMRData
{
public string xmlns { get; set; }
public string series { get; set; }
public string url { get; set; }
public string limit { get; set; }
public string offset { get; set; }
public string total { get; set; }
public clsRaceTable RaceTable { get; set; }
}

public class clsRaceTable
{
public string season { get; set; }
public List<clsRace> Races { get; set; }
}

public class clsRace
{
public string season { get; set; }
public string round { get; set; }
public string url { get; set; }
public string raceName { get; set; }
public clsCircuit Circuit { get; set; }
public string date { get; set; }
public string time { get; set; }
}

public class clsCircuit
{
public string circuitId { get; set; }
public string url { get; set; }
public string circuitName { get; set; }
public clsLocation Location { get; set; }
}

public class clsLocation
{
public string lat { get; set; }
[JsonProperty(PropertyName = "long")]
public string lon { get; set; }
public string locality { get; set; }
public string country { get; set; }
}

JSON Cannot Deserialize JSON array into type

Your Json data is wrapped in an array, so you need to deserialize to an array of your type

JsonConvert.DeserializeObject<BibleGatewayVerses[]>(jsonData);

Hope this helps

Cannot deserialize the JSON array (e.g. [1,2,3]) into type ' ' because type requires JSON object (e.g. {name:value}) to deserialize correctly

Your json string is wrapped within square brackets ([]), hence it is interpreted as array instead of single RetrieveMultipleResponse object. Therefore, you need to deserialize it to type collection of RetrieveMultipleResponse, for example :

var objResponse1 = 
JsonConvert.DeserializeObject<List<RetrieveMultipleResponse>>(JsonStr);

Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'portal.Models.SubProjectViewRecord'

Apparently looks like from the error that there are multiple items in the array that is why there is any array returned from the api reponse. you can use a List<T> for it and the code for it would be like :

List<Models.SubProjectViewRecord> oop = JsonConvert.DeserializeObject<List<Models.SubProjectViewRecord>>(objJson);

While your model would be below assuming that the json array elements are of json object with Id and Name members in it:

public class SubProjectViewRecord
{
public string Name { get; set; }
public int Id { get; set; }
}

JSON DeserializeObject Cannot deserialize the current JSON array (e.g. [1,2,3]) into type

You'll need to escape your quotes if the json is within a C# string. Example below:

using Newtonsoft.Json;
using System;
using System.Collections.Generic;

namespace ConsoleApp3
{
public class AgreementBreakdownObject
{
public string AgreementUID { get; set; }
public string ProjectUID { get; set; }
public string year { get; set; }
public List<AgreementBreakdownDataObject> data { get; set; }
}
public class AgreementBreakdownDataObject
{
public int month { get; set; }
public string B { get; set; }
public string P { get; set; }
public string F { get; set; }
}

class Program
{
static void Main(string[] args)
{
var data = JsonConvert.DeserializeObject<List<AgreementBreakdownObject>>(@"[
{
""AgreementUID"": ""a8ea9f59-82f3-4799-b684-06a2cd95d9a1"",
""ProjectUID"": ""851D12CE-7DC3-E511-9C58-F0DEF17C096F"",
""year"": ""2016"",
""data"": [
{
""month"": 1,
""B"": ""4"",
""P"": ""1"",
""F"": ""1""
},
{
""month"": 2,
""B"": """",
""P"": """",
""F"": """"
}
]
}
]");
Console.WriteLine(JsonConvert.SerializeObject(data, Formatting.Indented));

Console.ReadKey();
}
}
}

Relevant excerpt from MSDN:

Use verbatim strings for convenience and better readability when the string text contains backslash characters, for example in file paths. Because verbatim strings preserve new line characters as part of the string text, they can be used to initialize multiline strings. Use double quotation marks to embed a quotation mark inside a verbatim string.



Related Topics



Leave a reply



Submit