Deserialize a nested DataSet from Json String with Json.NET - c#

I try to deserialize a DataSet from a JSON String with Json.NET. The Json String contents a status, message and the table that I want to use:
{
"status": "ok",
"message": "",
"table": [{
"column1": "value1",
"column2": "value2"
}, {
"column1": "value3",
"column2": "value4"
}]
}
Here my source:
public class RequestResult
{
public string status { get; set; }
public string message { get; set; }
}
...
var serializer = new JsonSerializer();
var sr = new StreamReader(response.GetResponseStream()).ReadToEnd();
var rr = JsonConvert.DeserializeObject<RequestResult>(sr);
// Without status and message the following works fine like here:
// http://www.newtonsoft.com/json/help/html/DeserializeDataSet.htm
DataSet dataSet = JsonConvert.DeserializeObject<DataSet>(sr);
gridControlMain.DataSource = dataSet.Tables["table"];
If I use the Json String without status and message it works fine, but I need status and message! Exception message from Json.NET is:
Unexpected JSON token when reading DataTable. Expected StartArray, got String. Path 'status', line 1, position 14.
How can I manage that?
Best regards

Your snippet shows an object with three properties, status, message and table. This object can't be deserialized as a DataTable. The table property can.
You can deserialize this snippet to a class with two string and one DataTable property, eg:
class MyTableUtilClass
{
public string Status{get;set;}
public string Message {get;set;}
public DataTable Table{get;set;}
}
var myUtil=JsonConvert.DeserializeObject<MyTableUtilClass>(jsonText);
DataTable myTable=myUtil.Table;

Related

How to remove remove key value from JSON using C#

I have a json response that I will need to edit before binding to a GridView. I'm using Newtonsoft JSON library. The data binds perfectly to the GridView when "values" are removed from the string. When values is added to the json response I get this error message.
Message=Unexpected JSON token when reading DataTable. Expected StartArray, got StartObject. Path '', line 1, position 1.
How can I remove "values" from jsonResponse before binding to GridView?
<asp:GridView ID="gvJson" runat="server" BackColor="White" BorderColor="#3399ff"
BorderStyle="Dotted" BorderWidth="1px" CellPadding="3" GridLines="Both" AutoGenerateColumns="true"></asp:GridView>
var jsonResponse = "{\"values\":[{\"id\":\"201\",\"name\":\"Zack\"},{\"id\":\"158\",\"name\":\"Kim\"},{\"id\":\"254\",\"name\":\"Scott\"}]}";
DataTable dataTable = JsonConvert.DeserializeObject<DataTable>(jsonResponse);
gvJson.DataSource = dataTable;
gvJson.DataBind();
My JSON:
{
"values": [
{
"id": "201",
"name": "Zack"
},
{
"id": "158",
"name": "Kim"
},
{
"id": "254",
"name": "Scott"
}
]
}
There are a couple of ways to do this..
You could use JObject
var dataTable = JObject
.Parse(jsonResponse)["values"]
.ToObject<DataTable>();
Or as been suggested, fully deserialize the Json to concrete classes and bind the array to control directly
Given
public class Value
{
public string id { get; set; }
public string name { get; set; }
}
public class Root
{
public List<Value> values { get; set; }
}
Usage
var root = JsonConvert.DeserializeObject<Root>(jsonResponse);
// your Data , root.Data
Instead of removing the values from JSON response, you should be able to do something like
var jsonObject = JObject.Parse(jsonResponse);
var dataTable = JsonConvert.DeserializeObject<DataTable>(jsonObject["values"].ToString());
gvJson.DataSource = dataTable;
gvJson.DataBind();

How to read JSON data column in Entity Framework?

I created a database with JSON columns in mysql.
API defined in Swagger.
Writing JSON to the database works without problems, but when reading, the value of the JSON field is shown as an escaped string and not JSON
Here is part of model:
/// <summary>
/// Gets or Sets Doc
/// </summary>
[DataMember(Name="doc")]
public Dictionary<string, string> Doc { get; set; }
I also tried with string type and Dictionary<string, object> but unsuccessful.
Get method is here:
public virtual IActionResult GetDataById([FromRoute][Required]int? dataId, [FromRoute][Required]string jsonNode)
{
if(_context.Data.Any( a => a.Id == dataId)) {
var dataSingle = _context.Data.SingleOrDefault( data => data.Id == dataId);
return StatusCode(200, dataSingle);
} else {
return StatusCode(404);
}
}
}
And resulting JSON resposne looks like this one:
{
"field1": "value1",
"field2": "value2",
"doc": "{\"key1\":\"value1\",\"key2\":\"value2\",\"key3\":{\"subkey3-1\":\"value3-1\",\"subkey3-2\":\"value3-2\"}}"
}
but correct JOSN should be linke this one:
{
"field1": "value1",
"field2": "value2",
"doc": {
"key1":"value1",
"key2":"value2",
"key3":{
"subkey3-1":"value3-1",
"subkey3-2":"value3-2"
}
}
}
If I try to return just "Doc" (JSON) field, response JSON is properly formatted.
I tried different serialization/deserialization but unsuccessful.
If I have public Dictionary<string, string> Doc { get; set; } in Model I got error:
fail: Microsoft.AspNetCore.Server.Kestrel[13]
Connection id "0HM1VN0F0I0IM", Request id "0HM1VN0F0I0IM:00000001": An unhandled exception was thrown by the application.
System.InvalidOperationException: The property 'Data.Doc' is of type 'Dictionary<string, string>' which is not supported by current database provider. Either change the property CLR type or ignore the property using the '[NotMapped]' attribute or by using 'EntityTypeBuilder.Ignore' in 'OnModelCreating'
and for public string Doc { get; set; } in Model I got "doc" field value as escaped string as I mention above.
What would be the best way to go about this?
A few days later I found a resolution.
You need a one helper method that converts a string to object, exactly JObject if you use Json.NET - Newtonsoft.
public static JObject getJsonOutOfData (dynamic selectedData)
{
var data = JsonConvert.SerializeObject(selectedData);
var jsonData = (JObject)JsonConvert.DeserializeObject<JObject>(data);
if (selectedData.Doc != null) {
JObject doc = JObject.Parse(selectedData.Doc);
JObject docNode = JObject.Parse("{\"doc\":" + doc.ToString() + "}");
var jsonDoc = (JObject)JsonConvert.DeserializeObject<JObject>(docNode.ToString());
jsonData.Property("doc").Remove();
jsonData.Merge(jsonDoc, new JsonMergeSettings
{
MergeArrayHandling = MergeArrayHandling.Union
});
}
return jsonData;
}
and call it in the IActionResult method
public virtual IActionResult GetDataById([FromRoute][Required]int? accidentId, [FromRoute][Required]string jsonNode)
{
if (_context.Data.Any( a => a.Id == accidentId)) {
var accidentSingle = _context.Data.SingleOrDefault( accident => accident.Id == accidentId);
var result = getJsonOutOfData(accidentSingle);
return StatusCode(200, result.ToString());
} else {
return StatusCode(404);
}
}
In order to use unescaped JSON in POST/PUT requests, you should have a string type as a property type of JSON filed in the database model, and create an additional "request" model and set JSON property as "Object" type:
Database Model:
[DataMember(Name="doc")]
public string Doc { get; set; }
Request Model:
[DataMember(Name="doc")]
public Object Doc { get; set; }
For example Create model looks like:
public virtual IActionResult CreateData([FromBody]DataCreateRequest body)
{
var accidentObject = new Data() {
ColumnOne = body.ColumnOne,
ColumnTwo = body.ColumnTwo,
ColumnThree = body.ColumnThree,
Doc = (bodyDoc != null) ? body.Doc.ToString() : "{}"
};
_context.Data.Add(accidentObject);
_context.SaveChanges();
return StatusCode(200, getJsonOutOfData(accidentObject));
}

Unable to handle Collection Deserealization Error

I'm trying to handle deresealize an object that does not comply with some of my classes. I wold like the code to execute and fail only on the invalid attributes but the deserealization method is returning a null Object.
I am using this method in a generic utility class that deserealizes some string to any given type.
From the test code, the error handler works correctly on invalid dates and other invalid types and returns the object with the default .NET initialization values.
If I change (or comment) the Items collection in the sub object, the code works.
string json = "{\"Id\":8,\"CreatedByUserId\":0,\"CreatedOnDate\":\"2019X-Y02Z-W06T18:A51:05.783\",\"LastModifiedByUserId\":1,\"LastModifiedOnDate\":\"2019-03-12T17:00:34.82\",\"OperationData\":{\"IsActive\":true,\"Items\":[{\"_Id\":1,\"Id_Value\":0,\"Id\":1},{\"_Id\":2,\"Id\":2},{\"Id\":1,\"IsDeleted\":false,\"Content\":{}}]}}";
TestType test = DeserealizeContent(json);
/*The convertion utility*/
private static TestType DeserealizeContent(string data)
{
var settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
MissingMemberHandling = MissingMemberHandling.Ignore,
Error = HandleDeserializationError
};
var r = JsonConvert.DeserializeObject<TestType>(data, settings);
return r;
}
public static void HandleDeserializationError(object sender, ErrorEventArgs errorArgs)
{
errorArgs.ErrorContext.Handled = true;
}
/*Supporting types*/
public class TestType {
public int Id { get; set; }
public DateTime CreatedOnDate { get; set; }
public int CreatedByUserId { get; set; }
public string Instructions { get; set; }
public OperationDataType OperationData {get;set;}
}
public class OperationDataType {
public bool IsActive { get; set; }
public List<int> Items { get; set; }
}
I was expecting the error handler to catch handle the exception and continue with the process but instead the deserealization just returns null in the end.
If I change List Items to List Items the result is correctly parsed.
My expected result wold be:
{
"Id": 8,
"CreatedByUserId": 0,
"CreatedOnDate": null,
"LastModifiedByUserId": 1,
"LastModifiedOnDate": "2019-03-12T17:00:34.82",
"OperationData": {
"IsActive": true,
"Items": null
}
}
EDIT - workaround
The suggestion from Yair (bellow) works.
Changing from List to List works as expected and the exception get handle correctly.
The items in your json is not an array of int so how you want it to be List?
Items is array of objects look at the json formated:
{
"Id": 8,
"CreatedByUserId": 0,
"CreatedOnDate": "2019X-Y02Z-W06T18:A51:05.783",
"LastModifiedByUserId": 1,
"LastModifiedOnDate": "2019-03-12T17:00:34.82",
"OperationData": {
"IsActive": true,
"Items": [{
"_Id": 1,
"Id_Value": 0,
"Id": 1
}, {
"_Id": 2,
"Id": 2
}, {
"Id": 1,
"IsDeleted": false,
"Content": {}
}
]
}
}
You can do one of three things:
handle the json content as string or map it so the items will be int array [1,2,3].
create item class for the items that include all the fields you need then extract the int that you want.
get it as object like you do now and use reflection () for getting the int you want.
you can use this function for reflection:
public static object GetPropValue(object src, string propName)
{
return src.GetType().GetProperty(propName).GetValue(src, null);
}
and then use it like this:
GetPropValue(test.OperationData.items[0], "Id")
EDIT
you can use this to deserialize the json in generic way:
Newtonsoft.Json.Linq.JObject jsonDeserialized = (Newtonsoft.Json.Linq.JObject)Newtonsoft.Json.JsonConvert.DeserializeObject<object>(json);
and then
you can map it manually or with automapper to the new TestType test that you want without the items in it.
you can get the values like this:
test.Id = jsonDeserialized["Id"];
test.CreatedByUserId = jsonDeserialized["CreatedByUserId"];
and so on
according to your last comment i find that if i changed the List<int> Items to List<long> Items it works as you wanted. it is something with primitive types and the parsing that deserialize do.

How can I print Json values in a gridview?

I am trying to print values from a Json string in a gridview with C# using Visual Studio 2017. The problem is that I can't get the specific Value to a single word.
Here is my code:
string link = #"http://alexander.*************/test.php";
string json = new WebClient().DownloadString(link);
JObject jObject = JObject.Parse(json);
I want to print both Values from "Name" in the gridview, but how?
The Names has to put in this Item list:
myItems = (array?);
string test2 = test1.ToString(Formatting.Indented);
ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, myItems);
GridviewCoins.Adapter = adapter;
And finally the json string is:
{
"Coins": [[{
"Name": "007",
"Id": "5294",
}], [{
"Name": "1337",
"Id": "20824",
}
There is a couple problems here, first is that your Coins Property is an array of arrays, and that you jsonObject is not complete it should look like :
"{ "Coins": [[{ "Name": "007", "Id": "5294", } ], [{ "Name": "1337", "Id": "20824", }]]}";
That said if this is a copy paste error I would do some thing like:
public IEnumerable<Coin> GetCoins(string json)
{
var jObject = JObject.Parse(json);
var coinPropery = jObject["Coins"] as JArray;
var coins = new List<Coin>();
foreach (var property in coinPropery)
{
var propertyList = JsonConvert.DeserializeObject<List<Coin>>(property.ToString());
coins.AddRange(propertyList);
}
return coins;
}
and the coin object:
public class Coin
{
public int Id { get; set; }
public string Name { get; set; }
}
when then you have a c# object and you can do what ever you want with it.
EDIT:
You can add to gridview by following Click here

A better way to deserialize JSON?

I am posting to an API that may return either 1 of the following 2 formats of JSON strings:
{
"MessageType": 6,
"Message": "Unable to SAVE new record. Invalid posted data."
}
or
{
"Model": {
"Id": "1-6Q0RZ9",
...
},
"ResponseResult": {
"MessageType": 10,
"Message": "Successfully saved, Record Id = 1-6Q0RZ9"
}
}
I need to retrieve the results from MessageType and have tried every if condition I can think of to read the results, because the syntax or retrieving the key:value is different for each JSON string, and there are no other flags to trigger one or the other. So the code I used is:
string result = eml.PostData("API/Save", dataJSON.ToString());
var returnresult = new JavaScriptSerializer().Deserialize<dynamic>(result);
try {
var responseresults = returnresult["ResponseResult"];
rr = responseresults["MessageType"];
rrtxt = responseresults["Message"];
} catch (Exception ex) {
rr = returnresult["MessageType"];
rrtxt = returnresult["Message"];
}
Which works great. If there is a valid Db post it returns the second JSON which is parsed correctly by the TRY statement, if not it throws a "key not found" error and parses the returned string in the CATCH statement (the first JSON example). Obviously this is horrible code but I cannot think of another way to do this, and I was wondering if anyone had suggestions? (please?)
Thanx in advance.
How about deserializing the response to an object with all of the properties on each return type and then just checking the values?
public class ReturnObject
{
public YourModel Model {get;set;}
public ResultObject ResponseResult {get;set;}
public int? MessageType {get;set;}
public string Message {get;set;}
}
string result = eml.PostData("API/Save", dataJSON.ToString());
var returnresult = new JavaScriptSerializer().Deserialize<ReturnObject>(result);
{
if(returnresult.MessageType.HasValue)
{
var messageType = returnResult.MessageType.Value;
etc etc.
}
}

Categories

Resources