Cannot deserialize the current JSON array (e.g. [1,2,3]) into type
It looks like the string contains an array with a single MyStok
object in it. If you remove square brackets from both ends of the input, you should be able to deserialize the data as a single object:
MyStok myobj = JSON.Deserialize<MyStok>(sc.Substring(1, sc.Length-2));
You could also deserialize the array into a list of MyStok
objects, and take the object at index zero.
var myobjList = JSON.Deserialize<List<MyStok>>(sc);
var myObj = myobjList[0];
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; }
}
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'TenantManagementWebApi.Entities.Tenant
Your model
gets posted in its string
representation, which is not JSON
.await modelContent.ReadAsStringAsync()
will return a string in array-notation: [object Object]
.
You'll have to take care of the JSON serialization yourself.
Replace
data.append("model", { "TenantId": this.state.TenantId, "TenantUrl": this.state.TenantUrl, "TenantPassword": this.state.TenantPassword });
with
data.append("model", JSON.stringify({ "TenantId": this.state.TenantId, "TenantUrl": this.state.TenantUrl, "TenantPassword": this.state.TenantPassword }));
Cannot deserialize the current JSON array (e.g. [1,2,3])
I think the problem you're having is that your JSON is a list of objects when it comes in and it doesnt directly relate to your root class.
var content
would look something like this (i assume):
[
{
"id": 3636,
"is_default": true,
"name": "Unit",
"quantity": 1,
"stock": "100000.00",
"unit_cost": "0"
},
{
"id": 4592,
"is_default": false,
"name": "Bundle",
"quantity": 5,
"stock": "100000.00",
"unit_cost": "0"
}
]
Note: make use of http://jsonviewer.stack.hu/ to format your JSON.
So if you try the following it should work:
public static List<RootObject> GetItems(string user, string key, Int32 tid, Int32 pid)
{
// Customize URL according to geo location parameters
var url = string.Format(uniqueItemUrl, user, key, tid, pid);
// Syncronious Consumption
var syncClient = new WebClient();
var content = syncClient.DownloadString(url);
return JsonConvert.DeserializeObject<List<RootObject>>(content);
}
You will need to then iterate if you don't wish to return a list of RootObject
.
I went ahead and tested this in a Console app, worked fine.
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; }
}
Related Topics
Global Setting for Asnotracking()
How to Configure Many to Many Relationship Using Entity Framework Fluent API
Changing Read Only Properties with Reflection
How Does Deferred Linq Query Execution Actually Work
Linq - What Is the Quickest Way to Find Out Deferred Execution or Not
C#: How to Edit Items and Subitems in a Listview
Selecting the Size of a System.Drawing.Icon
Local Database, I Need Some Examples
Authentication with Old Password No Longer Supported, Use 4.1 Style Passwords
How to Sort Databound Datagridview Column
Process.Start to Open an Url, Getting an Exception
How to Cast a Generic Enum to Int
Topmost Form, Clicking "Through" Possible
Google Maps V3 Geocoding Server-Side
Client Id for Property (ASP.NET MVC)
Where Are Clr-Defined Methods Like [Delegate].Begininvoke Documented