C# - De-serialize class having byte array from string [duplicate] - c#

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.

Related

How to print converted object from QuickType.io? [duplicate]

This question already has answers here:
What is the best way to dump entire objects to a log in C#?
(16 answers)
Why writing items to console writes only namespace and class name instead of data? [duplicate]
(5 answers)
Class List Keeps Printing Out As Class Name In Console?
(4 answers)
C#: Printing all properties of an object [duplicate]
(9 answers)
Closed 4 years ago.
I'm trying to find out how QuickType.io works, however I cannot find a way to show my converted results.
I converted a JSON file to C# and got a satisfactory output:
public partial class Welcome
{
[JsonProperty("relatiesoort")]
public string[] Relatiesoort { get; set; }
[JsonProperty("modifiedOn")]
public DateTimeOffset ModifiedOn { get; set; }
[JsonProperty("relatiecode")]
public long Relatiecode { get; set; }
[JsonProperty("naam")]
public string Naam { get; set; }
}
I think the Welcome class can be used to do what I want:
public partial class Welcome
{
public static Welcome[] FromJson(string json) => JsonConvert.DeserializeObject<Welcome[]>(json, APIconnectV2.JSON.Converter.Settings);
}
This is the code I use to print the results:
var result = httpClient.GetAsync(new Uri("http://hidden.com')")).Result;
var json = result.Content.ReadAsStringAsync().Result;
dynamic jObject = JArray.Parse(json);
System.Diagnostics.Debug.WriteLine(Welcome.FromJson(json)[0]);
However, the output I receive is:
APIconnectV2.JSON.Welcome
Any number greater than 0 results in an out of bounds error. I'm not even sure if this is the correct way to print my output, however I cannot find a lot of documentation surrounding QuickType.

Parse json name begins with number in C# by Json.net - Newtonsoft [duplicate]

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; }
}

Is there a way to decorate an int property so that it serializes as a string? [duplicate]

This question already has answers here:
How to make JSON.Net serializer to call ToString() when serializing a particular type?
(4 answers)
Closed 3 years ago.
Is it possible to create a decorator for a property of a class of type int so it serializes as a string?
I have
public class MyClass
{
[SerializeAsString] //this is what I want
public int StreetCode { get; set; }
}
so when I call
var jsonRequest = JsonConvert.SerializeObject(myClass);
I want it to output the value between quotes rather than as an int without quotes.
This requires a custom converter based on Newtonsoft.Json.Converter to be created.
Then you would use the converter like so
[JsonConverter(typeof(ToStringConverter))]
public int StreetCode { get; set; }

Colon in C# List definition [duplicate]

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; }

.NET classes needed to deserialize JSON string [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Parse JSON in C#
I'm trying to deserialize a JSON string from the openlibrary.org in an ASP.NET (4.5) web application using JSON.NET.
My aim is to be able to read the 5 properties below from a single .Net object.
The example JSON
I have is:
{"ISBN:0201558025":
{
"bib_key": "ISBN:0201558025",
"preview": "noview",
"thumbnail_url": "http://covers.openlibrary.org/b/id/135182-S.jpg",
"preview_url": "http://openlibrary.org/books/OL1429049M/Concrete_mathematics",
"info_url": "http://openlibrary.org/books/OL1429049M/Concrete_mathematics"
}
}
I can get it to work fine without the first line, but I'm a bit lost trying to work out how I should structure my classes.
I'm new to JSON and haven't played with C#/VB.NET in a few years so getting very lost.
Update
I have the following code:
Dim url = "http://openlibrary.org/api/books?bibkeys=ISBN:0201558025&format=json"
Dim webClient = New System.Net.WebClient
Dim json = webClient.DownloadString(url)
Dim book As Book = JsonConvert.DeserializeObject(Of Book)(json)
And the class Book:
Public Class Book
Public bib_key As String
Public preview As String
Public preview_url As String
Public info_url As String
End Class
However, book turns up empty.
There is a website called json2csharp - generate c# classes from json:
public class RootObject
{
public string bib_key { get; set; }
public string preview { get; set; }
public string thumbnail_url { get; set; }
public string preview_url { get; set; }
public string info_url { get; set; }
}
The json format is a little off, remove the {"ISBN:0201558025": since you have the ISBN as the bib_key
Try using JSON.Net
or
JavaScriptSerializer Class
or
DataContractSerializer class
I think it can be deserialized as a Dictionary.
new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<Dictionary<string, BookInfoClass>>(jsonString);

Categories