This question already has answers here:
How can I parse a JSON string that would cause illegal C# identifiers?
(3 answers)
Convert JSON with illegal characters in property name [duplicate]
(1 answer)
Closed 2 years ago.
{
"0": {
"no": "tenon",
"title": "ProdtesterTITLE439",
"stock": 12
},
"success": 1
}
I want to desrialize those json.The problem is I cant create class with name 0 in c sharp.i have tried
[JsonObject(Title = "0")]
and
[DataContract(Name ="0")]
Not one of them are worked.
Good news! Your root object doesn't have a name, so you don't need to create a class with that name. 0 is a property of the root object.
Of course, 0 isn't a valid property name in C# either. That's where JsonPropertyAttribute comes in:
public class RootObject
{
[JsonProperty("0")]
public MyData Data {get;set;}
public bool Success {get;set;}
}
public class MyData
{
public int Stock {get;set;}
// other properties
}
Related
This question already has answers here:
Deserialize json that has some property name starting with a number
(2 answers)
Closed 5 years ago.
I need to parse JSON file in C# code by using JSON.net (Newtonsoft)
But json file I receive begins as this:
{"3h":3}
the variable name begins with number but c# can't do like this.
How can I set the value in the right way? Should I swap the variable name by my self? That would make very dirty code.
Thank you.
You can do this little focus with mapping:
class Program
{
static void Main(string[] args)
{
string jsonInput = #"{""3h"":3}";
var result = (myJsonObj)JsonConvert.DeserializeObject<myJsonObj>(jsonInput);
Console.WriteLine(result.MyProperty);
}
}
public class myJsonObj
{
[JsonProperty(PropertyName = "3h")]
public string MyProperty { get; set; }
}
This question already has answers here:
Newtonsoft.Json deserializing base64 image fails
(3 answers)
Closed 5 years ago.
I have a class
[Serializable]
public class DocumentMetadataBEO
{
public Guid ItemId { get; private set; }
public byte[] HashValue { get; set; }
}
I am receiving string having both of the above value.
However when i try to deserialize as below -
documentMetadata = JsonConvert.DeserializeObject<DocumentMetadataBEO>(responseFromServer);
HashValue property is getting set null. How can I deserialize it?
Here is the Json format, we get from server
"{
\"ItemId\":\"a1606584-9b9e-4bba-845f-e775eb5ebda5",
\"HashValue\":\"UHj5WO00uD5MIeCEr0Bt8i03iMrqUfILky7wSiqIn7g=\
"}"
With a newer version of Json.NET, it is working out of the box.
This question already has answers here:
Parse JSON in C#
(7 answers)
Closed 7 years ago.
I want to retrieve the values from a Json array in my c# controller. Here's my json array :
{
name : "Name 1",
description : "Description 1",
count : 6
}
How i can retrieve each attribute in my c# controller ? i know how to do it if i deal with Objects but this json is not from a Object.
public IList[] getList(IList[] myList)
{
// How to get the value of the name and the description here ?
}
I will finally create a class with all the attributes i will need and then retrieve them the same way i do with an Object :
CustomClass myClass ;
string name = myClass.name
string description = myClass.description;
int count= myClass.count
...
This question already has answers here:
C# JSON.NET convention that follows Ruby property naming conventions?
(4 answers)
.NET NewtonSoft JSON deserialize map to a different property name
(6 answers)
Closed 7 years ago.
Is there an automatic way in Json.NET to automatically map JSON properties from lower case underscore to pascal case no underscore. I know you can add json attributes to each property but is that the only way?
My JSON is like this:
{
"first_name": "Roger",
"last_name": "The Shrubber"
}
Here is the class:
public class Customer
{
public FirstName { get; set; }
public LastName { get; set; }
}
This question already has answers here:
How can I parse a JSON string that would cause illegal C# identifiers?
(3 answers)
Closed 8 years ago.
I have to deserialize an JSON object array that contains a colon in root object name.
Does anyone know if I can achieve this somehow with colon in list definition?
public List<Customers> ngcp:customers { get; set; }
No, the proper way to do this would be to specify the name using an attribute or other method supported by json.net.
[JsonProperty(PropertyName = "ngcp:customers")]
public List<Customers> Customers { get; set; }