I am trying to deserialize an object in a Web Api from an object posted by angular. I am receiving an error: Error reading object reference '1'. Path 'Developers[0].DeveloperId', line 20, position 21
My Json object is (which has been validated as valid JSON):
{
"Id": 0,
"Name": "Name",
"OwnerId": 1,
"Description": "Description",
"Note": "My Notes",
"Stakeholders": [
{
"$id": "1",
"StakeholderId": 1,
"Name": "Mary",
"DateModified": "2018-02-21T12:28:15.023",
"DateCreated": "2018-02-21T12:28:15.023",
"$$hashKey": "object:3"
}
],
"Developers": [
{
"$id": "1",
"DeveloperId": 1,
"DeveloperName": "Joseph",
"DateModified": "2018-02-21T12:28:26.07",
"DateCreated": "2018-02-21T12:28:26.07",
"$$hashKey": "object:4"
}
]
}
I am trying to deserialize with:
var app = JsonConvert.DeserializeObject<Application>(request.ToString(), new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore
});
The developer class (which is similar to Stakeholder class)
public class Developer : IModificationHistory
{
public int DeveloperId { get; set; }
[Required]
public string DeveloperName { get; set; }
[JsonIgnore]
public virtual List<Application> Applications { get; set; }
public DateTime DateModified { get; set; }
public DateTime DateCreated { get; set; }
}
The application class is simply:
public class Application
{
public int Id { get; set; }
[Required]
public string Name { get; set; }
[Required]
public string Description { get; set; }
public string Note { get; set; }
public virtual List<Stakeholder> Stakeholders { get; set; }
public int OwnerId { get; set; }
public virtual List<Developer> Developers { get; set; }
}
The javascript I use to call this post is:
var data =
{
Id: vm.appId,
Name: vm.applicationName,
OwnerId: vm.owner.DeveloperId,
Description: vm.applicationDescription,
Note: vm.note,
Stakeholders: vm.selectedStakeholders,
Developers: vm.selectedDevelopers
};
$http.post("/api/Application/Post/", JSON.stringify(data))
The Stakeholders List gets filled properly, but the Developers list does not. If I put developers in the list before stakeholders, then developers list gets filled properly and stakeholders does not. Any suggestions would be greatly appreciated!
The problem is with the same value of $id, both are set to 1, see inner exception:
{"A different value already has the Id '1'."}
I just changed its value to 2 and it is working fine:
{
"Id": 0,
"Name": "Name",
"OwnerId": 1,
"Description": "Description",
"Note": "My Notes",
"Stakeholders": [
{
"$id": "1",
"StakeholderId": 1,
"Name": "Mary",
"DateModified": "2018-02-21T12:28:15.023",
"DateCreated": "2018-02-21T12:28:15.023",
"$$hashKey": "object:3"
}
],
"Developers": [
{
"$id": "2",
"DeveloperId": 1,
"DeveloperName": "Joseph",
"DateModified": "2018-02-21T12:28:26.07",
"DateCreated": "2018-02-21T12:28:26.07",
"$$hashKey": "object:4"
}
]
}
Here is the screenshot of my output:
Related
I am trying to deserialize a json string from an api.
All works except for a nested item - Location which always ends up null
This is the string structure:
[
{
"CompanyProfile": "<p>A&B Inc etc...</p>",
"Address": "56 Test Street, Test, UK",
"Location": {
"Latitude": 61.52787,
"Longitude": -4.32095,
"Zoom": 13,
"Icon": null,
"Format": 0,
"Api": null,
"SearchTyped": null
},
"Id": 1723,
"Name": "A&B Inc"
},
{
"CompanyProfile": "<p>B&C Inc etc...</p>",
"Address": "57 Test Street, Test, UK",
"Location": {
"Latitude": 61.2122,
"Longitude": -4.31111,
"Zoom": 13,
"Icon": null,
"Format": 0,
"Api": null,
"SearchTyped": null
},
"Id": 1723,
"Name": "B&C Inc"
},
]
These are the classes to map to:
public class MemberDto
{
public int Id { get; set; }
public string? Name { get; set; }
public string? CompanyProfile { get; set; }
public string? Address { get; set; }
public Location? Location { get; internal set; }
}
public class Location
{
public decimal Latitude { get; set; }
public decimal Longitude { get; set; }
}
This is the deserialize code:
var result = await response.Content.ReadAsStringAsync();
var members = JsonConvert.DeserializeObject<List<MemberDto>>(result);
I know I can use ReadFromJsonAsync<List<MemberDto>>() as well but using ReadFromString so I can check the json before deserializing. Anyway the result is exactly the same for ReadFromJsonAsync.
Everything except Location is deserialized successfully
Anyone know what the issue is?
Remove the internal access modifier in the setter of Location.
public Location? Location { get; set; }
I want to deserialize this json to a List of Product objects but i get this error:
Newtonsoft.Json.JsonSerializationException: Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[ShoppingList.Product]' 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 my code:
{
"records": [
{
"id": "60",
"name": "Rolex Watch",
"description": "Luxury watch.",
"price": "25000",
"category_id": "1",
"category_name": "Fashion"
},
{
"id": "48",
"name": "Bristol Shoes",
"description": "Awesome shoes.",
"price": "999",
"category_id": "5",
"category_name": "Movies"
},
{
"id": "42",
"name": "Nike Shoes for Men",
"description": "Nike Shoes",
"price": "12999",
"category_id": "3",
"category_name": "Motors"
}
]
}
public class Product
{
public int id { get; set; }
public string name { get; set; }
public string description { get; set; }
public decimal price { get; set; }
public int category_id { get; set; }
public string category_name { get; set; }
}
public async Task<List<Product>> GetProductsAsync()
{
Products = new List<Product>();
var uri = new Uri("https://hostname/api/product/read.php");
try
{
var response = await client.GetAsync(uri);
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
Products = JsonConvert.DeserializeObject<List<Product>>(content);
}
}
catch (Exception )
{
throw;
}
return Products;
}
Your Json is not a List<Product>, its an object with a single property called records which is a List<Product>.
So your actual C# model is this:
public class RootObject
{
public List<Product> records { get; set; }
}
And you deserialize like this:
RootObject productsRoot = JsonConvert.DeserializeObject<RootObject>(content);
The issue is that your response JSON you've provided is an object which contains a list, but you are trying to deserialize the data into a straight list. I think all you need to do is add a second class which contains the list into your code, then deserialize using that list.
public class Product
{
public int id { get; set; }
public string name { get; set; }
public string description { get; set; }
public decimal price { get; set; }
public int category_id { get; set; }
public string category_name { get; set; }
}
public class RootProduct
{
public List<Product> Products {get;set;}
}
Then you change your deserialization portion from List to
RootProduct response = JsonConvert.DeserializeObject<RootProduct>(content);
The usage would then simply be products. response.Products to access the list of items.
The problem is your json structure. Below is an example of a json array - notice i removed the property "records".
[
{
"id": "60",
"name": "Rolex Watch",
"description": "Luxury watch.",
"price": "25000",
"category_id": "1",
"category_name": "Fashion"
},
{
"id": "48",
"name": "Bristol Shoes",
"description": "Awesome shoes.",
"price": "999",
"category_id": "5",
"category_name": "Movies"
},
{
"id": "42",
"name": "Nike Shoes for Men",
"description": "Nike Shoes",
"price": "12999",
"category_id": "3",
"category_name": "Motors"
}
]
Given this data
[
{
"id": "60",
"name": "Rolex Watch",
"description": "Luxury watch.",
"price": "25000",
"category_id": "1",
"category_name": "Fashion"
},
{
"id": "48",
"name": "Bristol Shoes",
"description": "Awesome shoes.",
"price": "999",
"category_id": "5",
"category_name": "Movies"
},
{
"id": "42",
"name": "Nike Shoes for Men",
"description": "Nike Shoes",
"price": "12999",
"category_id": "3",
"category_name": "Motors"
}
]
And this class definition
public class Order
{
public int id { get; set; }
public string name { get; set; }
public string description { get; set; }
public string price { get; set; }
public string category_id { get; set; }
public string category_name { get; set; }
}
The code to convert it from JSON to a C# List<Order> is:
public static List<Order> Map(string json)
{
return Newtonsoft.Json.JsonConvert.DeserializeObject<List<Order>>(json);
}
I am trying to deserialize the following JSON String:
Link to JSON String
{
"result": 1,
"error": null,
"id": 0,
"data": {
"ASTEALTHYNODE01_0301_0_30": {
"css_class": "sensor rf digital humidity",
"default_name": "Humidity",
"device_type": "humidity",
"did": "30",
"gid": "0301",
"has_subdevice_count": 0,
"has_time_series": 1,
"is_actuator": 0,
"is_sensor": 1,
"is_silent": 0,
"last_data": {
"DA": 58,
"timestamp": 1355791804474
},
"meta": {},
"node": "ASTEALTHYNODE01",
"shortName": "Humidity",
"subDevices": {},
"vid": "0"
},
"ASTEALTHYNODE01_0301_0_31": {
"css_class": "sensor rf digital temperature",
"default_name": "Temperature",
"device_type": "temperature",
"did": "31",
"gid": "0301",
"has_subdevice_count": 0,
"has_time_series": 1,
"is_actuator": 0,
"is_sensor": 1,
"is_silent": 0,
"last_data": {
"DA": 26.6,
"timestamp": 1355791804475
},
"meta": {},
"node": "ASTEALTHYNODE01",
"shortName": "Temperature",
"subDevices": {},
"vid": "0"
},
"ASTEALTHYNODE01_0_0_1000": {
"css_class": "actuator cape led rgbled",
"default_name": "On Board RGB LED",
"device_type": "rgbled",
"did": "1000",
"gid": "0",
"has_subdevice_count": 0,
"has_time_series": 0,
"is_actuator": 1,
"is_sensor": 1,
"is_silent": 0,
"last_data": {
"DA": "22B42B",
"timestamp": 1355790209080
},
"meta": {},
"node": "ASTEALTHYNODE01",
"shortName": "On Board RGB LED",
"subDevices": {},
"vid": "0"
},
"ASTEALTHYNODE01_0_0_11": {
"css_class": "sensor serial rf rf433 receiver transmitter",
"default_name": "RF 433Mhz",
"device_type": "rf433",
"did": "11",
"gid": "0",
"has_subdevice_count": 1,
"has_time_series": 0,
"is_actuator": 1,
"is_sensor": 1,
"is_silent": 0,
"last_data": {
"DA": "010001010101010100010101",
"timestamp": 1355789891324
},
"meta": {},
"node": "ASTEALTHYNODE01",
"shortName": "RF 433Mhz",
"subDevices": {
"6l8At": {
"category": "rf",
"data": "011111110001010100110000",
"shortName": "Door Bell",
"type": "sensor"
}
},
"vid": "0"
}
}
}
I usually create the classes with help of: http://json2csharp.com and then I am doing something like this (Json.NET libary):
Collapse | Copy Code
var result = JsonConvert.DeserializeObject<MyObject>(jsonString);
But the number of devices and their names (example: ASTEALTHYNODE01_0_0_11) are unknown before I get the JSON string. How can i deserialize this ?
Thank you
This works for me, with your JSON, and using JSON.NET:
Payload payloadJsonNet = JsonConvert.DeserializeObject<Payload>(data);
System.Diagnostics.Debug.Assert
(
payloadJsonNet.data.ContainsKey("ASTEALTHYNODE01_0_0_11") &&
payloadJsonNet.data["ASTEALTHYNODE01_0_0_11"].subDevices.ContainsKey("6l8At") &&
payloadJsonNet.data["ASTEALTHYNODE01_0_0_11"].subDevices["6l8At"].shortName == "Door Bell"
);
... provided you have prepared the following POCOs (using the nifty json2csharp helper, for example):
public class Payload
{
public int result { get; set; }
public int id { get; set; }
public Error error { get; set; }
public Dictionary<string, Device> data { get; set; }
}
public class Device
{
public string css_class { get; set; }
public string default_name { get; set; }
public string device_type { get; set; }
public string did { get; set; }
public string gid { get; set; }
public int has_subdevice_count { get; set; }
public int has_time_series { get; set; }
public int is_actuator { get; set; }
public int is_sensor { get; set; }
public int is_silent { get; set; }
public LastData last_data { get; set; }
public Meta meta { get; set; }
public string node { get; set; }
public string shortName { get; set; }
public Dictionary<string, Device> subDevices { get; set; }
public string vid { get; set; }
}
public class LastData
{
public string DA { get; set; }
public long timestamp { get; set; }
}
public class Meta
{
}
public class Error
{
}
I guess you'll have to find out / infer from other JSON inputs what should be the properties for these last two guys (i.e., "Meta" and "Error").
Note also, the "long" type that needs to be used in:
public class LastData
{
public string DA { get; set; }
public long timestamp { get; set; }
}
Finally, just for the curious, my own parser deserializes it fine as well:
Payload payloadMyParser = new JsonParser().Parse<Payload>(data);
System.Diagnostics.Debug.Assert
(
payloadMyParser.data.ContainsKey("ASTEALTHYNODE01_0_0_11") &&
payloadMyParser.data["ASTEALTHYNODE01_0_0_11"].subDevices.ContainsKey("6l8At") &&
payloadMyParser.data["ASTEALTHYNODE01_0_0_11"].subDevices["6l8At"].shortName == "Door Bell"
);
'Hope this helps,
Your json "data" property looks like property with type Dictionary < string, SomeClass>, where SomeClass - is a class for
{
"css_class": "sensor rf digital humidity",
// skipped
"subDevices": {},
"vid": "0"
},
data structure, so you may define this property in MyObject class and use strong typed deserialization without any problem.
I have a json data when i'm trying to parse it returns error incorrect syntax please help me found the syntax error.
[{"isData":"Yes","Details":"[{"Id":"70","Name":"Test","FileName":"Uploaded","FileFormat":".mp4","FileType":"Video","FileDuration":"00:30:00 ","StartTime":"/Date(1372617000000)/","EndTime":"/Date(1372681771000)/","File":"2562013172331815635077778118152815.mp4"}]"}]
And this is the class that is used to serialize data, i am using javascript serializer
public enum Data
{
Yes,
No
}
public class MessageResponse()
{
public string isData { get; set; }
public string Details { get; set; }
}
List<MessageResponse> response = new List<MessageResponse>();
string strJson="[{"Id":"70","Name":"Test","FileName":"Uploaded","FileFormat":".mp4","FileType":"Video","FileDuration":"00:30:00 ","StartTime":"/Date(1372617000000)/","EndTime":"/Date(1372681771000)/","File":"2562013172331815635077778118152815.mp4"}]";
var newData = new MessageResponse
{
isData = Data.Yes.ToString(),
Details = strJson
};
response.Add(newData);
var jsonSerialiser1 = new JavaScriptSerializer();
string result = jsonSerialiser1.Serialize(response);
That's invalid JSON. The Details property is incorrectly formatted. You should remove the quotes around the value. It should be like this:
[
{
"isData": "Yes",
"Details": [
{
"Id": "70",
"Name": "Test",
"FileName": "Uploaded",
"FileFormat": ".mp4",
"FileType": "Video",
"FileDuration": "00:30:00 ",
"StartTime": "/Date(1372617000000)/",
"EndTime": "/Date(1372681771000)/",
"File": "2562013172331815635077778118152815.mp4"
}
]
}
]
or if you want Details to be a string property (representing JSON), which is kinda lame, you should properly escape the double quotes:
[
{
"isData": "Yes",
"Details": "[{\"Id\":\"70\",\"Name\":\"Test\",\"FileName\":\"Uploaded\",\"FileFormat\":\".mp4\",\"FileType\":\"Video\",\"FileDuration\":\"00: 30: 00\",\"StartTime\":\"/Date(1372617000000)/\",\"EndTime\":\"/Date(1372681771000)/\",\"File\":\"2562013172331815635077778118152815.mp4\"}]"
}
]
This structure you will be able to map to your current object model. But I would recommend you using the first approach.
Remove the " from the details data:
[{
"isData":"Yes",
"Details":
[{
"Id":"70",
"Name":"Test",
"FileName":"Uploaded",
"FileFormat":".mp4",
"FileType":"Video",
"FileDuration":"00:30:00",
"StartTime":"/Date(1372617000000)/",
"EndTime":"/Date(1372681771000)/",
"File":"2562013172331815635077778118152815.mp4"
}]
}]
Details should be of type class (i.e. user defined class) and it should hold all the properties.
public class Details
{ public int Id {get; set;} ... }
Firstly your json is invalid.
It should not have the " before and after the [ ]
[
{
"isData": "Yes",
"Details": [
{
"Id": "70",
"Name": "Test",
"FileName": "Uploaded",
"FileFormat": ".mp4",
"FileType": "Video",
"FileDuration": "00: 30: 00",
"StartTime": "/Date(1372617000000)/",
"EndTime": "/Date(1372681771000)/",
"File": "2562013172331815635077778118152815.mp4"
}
]
}
]
Secondly, your class could be improved to be:
public class MessageResponse
{
public string isData { get; set; }
public Details Details { get; set; }
}
public class Details
{
public int Id { get; set; }
public string Name { get; set; }
public string FileName { get; set; }
public string FileFormat { get; set; }
public string FileType { get; set; }
public string FileDuration { get; set; }
public string StartTime { get; set; }
public string EndTime { get; set; }
public string File { get; set; }
}
You would probably want to set up the correct data types though for things like Start Time etc...
I want to deserialize JSON data (using Newtonsoft) similar to following, and convert to a strongly-typed object/list in C#, but can't figure out how to define the class such that indexed references are converted to the referenced objects.
{
"Countries": [
{
"Name": "USA",
},
{
"Name": "UK",
},
{
"Name": "JAPAN",
},
],
"Authors": [
{
"DisplayName": "John Doe",
"RealName": "Not John Doe"
},
{
"DisplayName": "Jane Doe",
"RealName": "Not Jane Doe"
},
],
"Books": [
{
"Author": 0,
"Title": "A good read",
"PublishedCountries": "0,1",
},
{
"Author": 0,
"Title": "Another good read",
"PublishedCountries": "0,1",
},
{
"Author": 1,
"Title": "This one is even better",
"PublishedCountries": "0,1,2",
},
],
}
Ideally, I'd like to use classes similar to following:
public class Country
{
public string Name { get; set;}
}
public class AuthorDetails
{
public string DisplayName { get; set; }
public string RealName { get; set; }
}
public class Book
{
public AuthorDetails Author { get; set; }
public string Title { get; set; }
public IEnumerable<Country> PublishedCountries { get; set; }
}
public class ListOfBooks
{
public IEnumerable<Book> Books { get; set; }
}
And deserialize like this:
var listOfBooks = JsonConvert.DeserializeObject<ListOfBooks>(jsonAsString);
I'm stuck as how to tell Json.Net that the Author property in the book JObject is an index, rather than an integer. Same goes for the PublishedCountries (that's comma-separated list of indexes)
I can't see a way other than helping the deserialization process a little bit.
var dynObj = (JObject)JsonConvert.DeserializeObject(json);
var authors = dynObj["Authors"]
.Select(j => new AuthorDetails {
RealName = (string)j["RealName"],
DisplayName = (string)j["DisplayName"]
})
.ToList();
var countries = dynObj["Countries"]
.Select(j => new Country { Name = (string)j["Name"]})
.ToList();
var books = dynObj["Books"].Select(x => new Book
{
Author = authors[(int)x["Author"]],
Title = (string)x["Title"],
PublishedCountries = x["PublishedCountries"].ToString().Split(',')
.Select(i =>countries[int.Parse(i)])
.ToList()
})
.ToList();
public class Country
{
public string Name { get; set; }
}
public class AuthorDetails
{
public string DisplayName { get; set; }
public string RealName { get; set; }
}
public class Book
{
public AuthorDetails Author { get; set; }
public string Title { get; set; }
public List<Country> PublishedCountries { get; set; }
}
I believe what you want to do is 'preserve object references'.
You can mark-up your C# object with attributes to describe when to utilise a reference, rather than duplicate the content of the object all over again:
[JsonObject(IsReference = true)] (use on the type declaration)
However, your generated Javascript will not look quite like what you have demonstrated, take a look here to see how to do it.
http://james.newtonking.com/projects/json/help/?topic=html/PreserveObjectReferences.htm
Instead, your JSON will look more like this:
{
"Countries": [
{
"Name": "USA",
},
{
"Name": "UK",
},
{
"Name": "JAPAN",
},
],
"Authors": [
{
"DisplayName": "John Doe",
"RealName": "Not John Doe"
},
{
"DisplayName": "Jane Doe",
"RealName": "Not Jane Doe"
},
],
"Books": [
{
"$id": 1,
"Author": 0,
"Title": "A good read",
"PublishedCountries": "0,1",
},
{
"$ref": 1
},
{
"Author": 1,
"Title": "This one is even better",
"PublishedCountries": "0,1,2",
},
],
}