C# JsonConvert.DeserializeObject returns 0? - c#

I am attempting to write a Windows form app that pulls player data via a text file formatted in json but have run into issues. Any data I try to deserialize returns a 0 value in my text box.... I am new to coding and stuck, any help would be appreciated.
My code is as follows:
string json = System.IO.File.ReadAllText("textfile");
StatsCounts newStats = JsonConvert.DeserializeObject<StatsCounts>(json);
earningsTextBox.Text = Convert.ToString(newStats.earnings);
My Json Class Formating:
public class StatsCounts
{
public int earnings { get; set; }
public int firsts { get; set; }
public int seconds { get; set; }
public int thirds { get; set; }
public int starts { get; set; }
}
public class RootObject
{
public StatsCounts statsCounts { get; set; }
}
And my text file formatting:
{
"statsCounts": {
"earnings": 4576702,
"firsts": 131,
"seconds": 102,
"thirds": 80,
"starts": 590
}
}

You need to deserialize for the type RootObject, not StatsCounts, so it should read:
static void Main(string[] args)
{
var rootObject = JsonConvert.DeserializeObject<RootObject>("{\n \"statsCounts\": {\n \"earnings\": 4576702,\n \"firsts\": 131,\n \"seconds\": 102,\n \"thirds\": 80,\n \"starts\": 590\n }\n}");
var a = Convert.ToString(rootObject.statsCounts.earnings);
}

You have the correct classes, but you're not using them. Use RootObject to deserialize into.
Change to this:
var results = JsonConvert.DeserializeObject<RootObject>(json);
StatsCounts newStats = results.statsCounts;

Related

Convert JsonPatchDocument to string C#

I am using Newtonsoft.Json.JsonConvert.SerializeObject to convert a JsonPatchDocument<T> to string but it's value property (which is in JObject format) doesn't seem to be converted to string.
Here is what it looks like:
Here is the JSON I am using to create patchDocument object
[
{
"path": "/expenseLines/",
"op": "ReplaceById",
"value": {
"ExpenseLineId": 1,
"Amount": 4.0,
"CurrencyAmount": 4.0,
"CurrencyCode": "GBP",
"ExpenseDate": "2021-11-01T00:00:00",
"ExpenseType": "TAXI"
}
}
]
This JSON is successfully deserialized to JsonPatchDocument object but when I try to serialize it back to JSON, I lose value property (as shown in the picture by red arrows).
Any help would be appreciated :)
I can't reproduce your problem, can you provide more information? I got stuck during your second serialization. But I used using System.Text.Json to complete your needs, you can look at:
Model:
public class Test
{
public string path { get; set; }
public string op { get; set; }
public TestValue testValue { get; set; } = new TestValue();
}
public class TestValue
{
public int ExpenseLineId { get; set; }
public double Amount { get; set; }
public double CurrencyAmount { get; set; }
public string CurrencyCode { get; set; }
public DateTime ExpenseDate { get; set; }
public string ExpenseType { get; set; }
}
TestController:
[ApiController]
public class HomeController : Controller
{
[Route("test")]
public Object Index()
{
var patchDocument = new Test();
patchDocument.path = "/expenseLines/";
patchDocument.op = "ReplaceById";
patchDocument.testValue.ExpenseLineId = 1;
patchDocument.testValue.Amount = 4.0;
patchDocument.testValue.CurrencyAmount = 4.0;
patchDocument.testValue.CurrencyCode = "GBP";
patchDocument.testValue.ExpenseDate = DateTime.Now;
patchDocument.testValue.ExpenseType = "TAXI";
var options = new JsonSerializerOptions { WriteIndented = true };
// var content = JsonConvert.SerializeObject(patchDocument);
// string content1 = JsonConvert.SerializeObject(patchDocument.Operations);
string jsonString = System.Text.Json.JsonSerializer.Serialize<Test>(patchDocument);
string jsonString1 = System.Text.Json.JsonSerializer.Serialize(patchDocument, options);
return jsonString;
}
Result:
Hope this helps you too.

How i can convert this JSON to Object in C#?

for two days I have been trying to understand how to move this JSON to an object in C#. I read a lot of topics and tried several ways to solve my problem, but still haven't solved it.
My JSON looks like this (cropped).
{
"data": [{
"id": 5643793,
"title": "It's a Title",
"description": "It's a Description.",
"tags": "#tag1 #tag2 #tag3 #tag4",
"source_url": "https:\/\/p.dw.com\/p\/3geny",
"vote_count": 120,
"bury_count": 17,
"comments_count": 33,
"related_count": 0,
"date": "2020-08-10 09:43:32",
"author": {
"login": "lnwsk",
"color": 2,
"avatar": "https:\/\/www.api.page.com\/cdn\/c3397992\/lnwsk_MfQz8MEQb2,q150.jpg"
},
"preview": "https:\/\/www.api.page.com\/cdn\/c3397993\/link_1597045214DgzqxRGEmy2UlpPZwaWfhI,w104h74.jpg",
"plus18": false,
"status": "promoted",
"can_vote": true,
"is_hot": false
}],
"pagination": {
"next": "https:\/\/api.page.com\/links\/promoted\/appkey\/X*******4y\/page\/2\/"
}
}
As you can see, there is an "element within an element" here (eg author or pagination (eg pagination I would like to get rid of)) and that is what gives me the most problem.
Here is my class where I have all the code to read the API:
using Newtonsoft.JSON;
public class PageAPI
{
public class Product
{
public string[] title { get; set; }
public double[] description { get; set; }
public string[] tags { get; set; }
public string[] source_url { get; set; }
public string[] vote_count { get; set; }
public string[] bury_count { get; set; }
public string[] comments_count { get; set; }
public string[] related_count { get; set; }
public string[] date { get; set; }
}
public async Task<Product> GetDataAsync()
{
string url = "https://api.page.com/";
string apisign = "6*********c1fe49a23f19ad6b2";
string requestParams = "links/promoted/appkey/X*******y";
Product obj = null;
// HTTP GET.
using (var client = new HttpClient())
{
// Setting Base address.
client.BaseAddress = new Uri(url);
// Setting content type.
client.DefaultRequestHeaders.Add("apisign", apisign);
// Initialization.
HttpResponseMessage response = new HttpResponseMessage();
// HTTP GET
response = await client.GetAsync(requestParams).ConfigureAwait(false);
// Verification
if (response.IsSuccessStatusCode)
{
try
{
// Reading Response.
string result = response.Content.ReadAsStringAsync().Result;
obj = JsonConvert.DeserializeObject<Product>(result);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
MessageBox.Show(ex.Message);
}
}
else
{
obj = null;
}
}
return obj;
}
}
in the Form where I want to get data from the "PageAPI" class I have:
private async void Form1_LoadAsync(object sender, EventArgs e)
{
var task = api.GetMainAsync();
task.Wait();
var data = task.Result;
label1.Text = data.title[0];
}
And... this doesn't works - on label1.Text = data.title[0]; i get error PageAPI.Product.title.get returned null
Thanks for any help!
You are missing the Root class that has "data" and "pagination" properties. Create Root class and deserialize to it and then get the data you need. Also, your Product class will have only strings.. not string[].
public class RootObject
{
public List<Product> data { get; set; }
}
public class Product
{
public string title { get; set; }
public double description { get; set; }
public string tags { get; set; }
public string source_url { get; set; }
public string vote_count { get; set; }
public string bury_count { get; set; }
public string comments_count { get; set; }
public string related_count { get; set; }
public string date { get; set; }
}
// and deserialize it
var rootObj = JsonConvert.DeserializeObject<RootObject>(result);
obj = rootObj.data.FirstOrDefault();
data object is an array ... you can loop through it to work with All the items. In above example, i used FirstOrDefault() to get the first item from the object.
Also note that when you access this data, you would not access it via [0]. Simply use
label1.Text = data.title;
Side Note
If you want the pagination property as well, create another class to get the name from pagination object.
public class RootObject {
public List<Product> data {get;set;}
public Pagination pagination {get;set;}
}
public class Pagination {
public string next {get;set; }
}
and when you deserialize your json, you would access the pagination by using,
Console.WriteLine(rootObj.pagination.next); // prints the url
How to get All the Product Names displayed
This is how you would go about getting a list of All the titles in the data object.
foreach (var product in rootObj.data)
{
Console.WriteLine(product.title);
Console.WriteLine(product.description);
Console.WriteLine(product.vote_count); // etc.
}
// Or you can create a list of all the titles from the rootObj using LINQ
List<string> allTitles = rootObj.data.Select(x => x.title).ToList();
I am not sure what you intend to do with the data you get... so not sure how to explain that piece.. but above example should give you an idea on how to iterate through all the products in the data object.

How to deserialize JSON without static key/property names

Hello I have problem with deserializing IRestResponse.Content JSON response.
{
"49":
{
"9345": "2018-10-11",
"106": null,
"107": "4222238842",
"108": "CompanyName",
"8210": "2018-11-11/1",
"110": "00-300",
"109": "Street",
"112": "Country",
"18418": null,
"18420": "S\u0141ON",
"18422": "OtherString",
"9338": null,
"111": "City"
}
}
I have tried some webpage's or built in VisualStudio converter but It gives my something like this.
public class Rootobject
{
public _49 _49 { get; set; }
}
public class _49
{
public string _9345 { get; set; }
public object _106 { get; set; }
public string _107 { get; set; }
public string _108 { get; set; }
public string _8210 { get; set; }
public string _110 { get; set; }
public string _109 { get; set; }
public string _112 { get; set; }
public object _18418 { get; set; }
public string _18420 { get; set; }
public string _18422 { get; set; }
public object _9338 { get; set; }
public string _111 { get; set; }
}
This look's ok but In my case those JSON files have dynamic property names and can have another "int" values. Also the nested content inside "49" can have less or more values.
I am especially interested in gathering "49" << this value to variable.
I also have tried something like this, but doesn't work either:
public class DeserializeJsonContent
{
public Dictionary<object, Dictionary<object, object>> values { get; set; }
}
Simplified code sample
public List<T> JSONDeserialize<T>(IRestResponse response) where T : new()
{
var responseData = client.Deserialize<List<T>>(response);
var ListDeserializedData = responseData.Data.ToList<T>();
return ListDeserializedData;
}
....
var response = rest.client.Execute(request);
if (response.IsSuccessful)
{
var obj = rest.JSONDeserialize<DeserializeJsonContent>(response);
}
obj has count 1 but vals = null
Edit after Solve:
I still have no idea why my deserialized class doesn't work in this case (I am using it in many other json deserialization response's)
Thanks to xdtTransform answer I have tried and all of those worked
var obj2 = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<object, object>>>(response.Content);
var obj3 = JsonConvert.DeserializeObject<Dictionary<string, Dictionary<string, string>>>(response.Content);
var obj4 = JsonConvert.DeserializeObject<Dictionary<int, Dictionary<string, string>>>(response.Content);
var obj5 = JsonConvert.DeserializeObject<Dictionary<object, Dictionary<string, string>>>(response.Content);
var obj6 = JsonConvert.DeserializeObject<Dictionary<object, Dictionary<object, object>>>(response.Content);
var obj7 = JsonConvert.DeserializeObject<Dictionary<object, Dictionary<int, object>>>(response.Content);
Then just
var value = obj2.First().Key;
Using Newtonsoft.Json, You can directly deserialize to Dictionary<string,Dictionary<string,string>> like :
var obj = JsonConvert.DeserializeObject<Dictionary<string,Dictionary<string,string>>>(input);
If you need it to be in your custom type, you should declare this type like :
public class WrapperType : Dictionary<string,Dictionary<string,string>>{}
Then the Deserialisation stay the same:
var obj = JsonConvert.DeserializeObject<WrapperType>(input);
Updated demo
Please deserialize response content
string data = response.Content;
var responseData = client.Deserialize<List<T>>(data);

Deserialize json array to c# list object

I am trying to parse a JSON response from a service to c# observation collection list object. The list object later can be used to showcase on the XAML page.
Here is the response from the service:
[
{
"orderId": 1,
"employeeId": "6364",
"orderTime": 1517583600000,
"orderCost": 90,
"comments": null,
"orderStatus": {
"orderStatusId": 1,
"orderStatusName": "Order Placed"
},
"orderedItems": [
{
"orderItemId": 1,
"orderQuantity": 1,
"orderItemCost": 50
},
{
"orderItemId": 2,
"orderQuantity": 1,
"orderItemCost": 40
}
]
},
{
"orderId": 2,
"employeeId": "6364",
"orderTime": 1517670000000,
"orderCost": 50,
"comments": null,
"orderStatus": {
"orderStatusId": 3,
"orderStatusName": "Order Delivered"
},
"orderedItems": [
{
"orderItemId": 3,
"orderQuantity": 1,
"orderItemCost": 50
}
]
}
]
The following is the model class :
namespace ServiceNew
{
public class OrderStatus
{
public int orderStatusId { get; set; }
public string orderStatusName { get; set; }
}
public class OrderedItem
{
[JsonProperty("orderItemId")]
public int orderItemId { get; set; }
[JsonProperty("orderQuantity")]
public int orderQuantity { get; set; }
[JsonProperty("orderItemCost")]
public int orderItemCost { get; set; }
}
public class Order
{
[JsonProperty("orderId")]
public int orderId { get; set; }
[JsonProperty("employeeId")]
public string employeeId { get; set; }
[JsonProperty("orderTime")]
public object orderTime { get; set; }
[JsonProperty("orderCost")]
public int orderCost { get; set; }
[JsonProperty("comments")]
public object comments { get; set; }
[JsonProperty("orderStatus")]
public OrderStatus orderStatus { get; set; }
[JsonProperty("orderedItems")]
public List<OrderedItem> orderedItems { get; set; }
}
}
The service is like this:
public class OrderService
{
public OrderService()
{
GetJson();
}
public async void GetJson()
{
if (NetworkCheck.IsInternet())
{
var client = new System.Net.Http.HttpClient();
var response = await client.GetAsync("here is thre URL");
string orderJson = await response.Content.ReadAsStringAsync(); //Getting response
Order ObjOrderList = new Order();
if (orderJson != " ")
{
Console.WriteLine("response is"+orderJson);
//exception occurs here all the time , and I need it to be a list
ObjOrderList = JsonConvert.DeserializeObject<Order>(orderJson);
}
Console.WriteLine("obj order list is"+ObjOrderList);
}
}
}
After trying with some changes to the deserialization the JSON array to c#, I was not able to succeed. Now there is an exception saying
Newtonsoft.Json.JsonSerializationException: <Timeout exceeded getting exception details>
And I am stuck at this for a long time, searched over StackOverflow and googled it but no fruitful solution for this.
I need to store the JSON data into a c# object and reproduce the same object in the XAML page as a list.
Thanks in advance!
I am sure that exception is not related to you JSON string but try to remove bin and obj from solution folder and then clean and rebuild solution.
but after resolving that you will get the below exception
Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'namespace.Order' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.....
Because your JSON string is List of Order so the deserialize would be change to :
List<Order> ObjOrderList = JsonConvert.DeserializeObject<List<Order>>(orderJson);
or in the other side you can also use JavaScriptSerializer like:
Order[] orderList = new JavaScriptSerializer().Deserialize<Order[]>(orderJson);
You are deserializing a List of Orders so you should deserialize like this:
...
List<Order> ObjOrderList;
...
ObjOrderList = JsonConvert.DeserializeObject<List<Order>>(orderJson);
...
Your JSON starts with a [ and ends with a ]. This means that your JSON represents an array of objects. These objects are:
First object
{
"orderId": 1,
"employeeId": "6364",
"orderTime": 1517583600000,
"orderCost": 90,
...
}
Second object
{
"orderId": 2,
"employeeId": "6364",
"orderTime": 1517670000000,
"orderCost": 50,
...
}
In your subconscious you knew it, in fact the name of your deserialized variable is ObjOrderList (highlight List).
So, just deserialize to an array/list of Order.
Example with list
var ObjOrderList = new List<Order>();
if (orderJson != " ")
{
//exception occurs here all the time , and I need it to be a list
ObjOrderList = JsonConvert.DeserializeObject<List<Order>>(orderJson);
}
Example with array
var ObjOrderList = new Order[] { };
if (orderJson != " ")
{
//exception occurs here all the time , and I need it to be a list
ObjOrderList = JsonConvert.DeserializeObject<Order[]>(orderJson);
}
Try this autogenerated code:
// To parse this JSON data, add NuGet 'Newtonsoft.Json' then do:
//
// using yourNameSpace;
//
// var orderResponse = OrderResponse.FromJson(jsonString);
namespace yourNameSpace
{
using System;
using System.Collections.Generic;
using System.Globalization;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
public partial class OrderResponse
{
[JsonProperty("orderId")]
public long OrderId { get; set; }
[JsonProperty("employeeId")]
public string EmployeeId { get; set; }
[JsonProperty("orderTime")]
public long OrderTime { get; set; }
[JsonProperty("orderCost")]
public long OrderCost { get; set; }
[JsonProperty("comments")]
public object Comments { get; set; }
[JsonProperty("orderStatus")]
public OrderStatus OrderStatus { get; set; }
[JsonProperty("orderedItems")]
public List<OrderedItem> OrderedItems { get; set; }
}
public partial class OrderStatus
{
[JsonProperty("orderStatusId")]
public long OrderStatusId { get; set; }
[JsonProperty("orderStatusName")]
public string OrderStatusName { get; set; }
}
public partial class OrderedItem
{
[JsonProperty("orderItemId")]
public long OrderItemId { get; set; }
[JsonProperty("orderQuantity")]
public long OrderQuantity { get; set; }
[JsonProperty("orderItemCost")]
public long OrderItemCost { get; set; }
}
public partial class OrderResponse
{
public static List<OrderResponse> FromJson(string json) => JsonConvert.DeserializeObject<List<OrderResponse>>(json);
}
code was generated using QuickType.io
I discarded the converter and some other extra classes.
You can change the Long type to int if you want.
To use it just call
var orderResponse = OrderResponse.FromJson(jsonString);
pass the response instead of jsonString
In this Code you can DeserializeObject json file:
using (StreamReader r = new StreamReader("D:/Source/ParsijooWeatherApi/ParsijooWeatherApi/cities2.json"))
{
string json = r.ReadToEnd();
List<jsonVariables> items = JsonConvert.DeserializeObject<List<jsonVariables>>(json);
dynamic array = JsonConvert.DeserializeObject(json);
foreach (var item in array)
{
Console.WriteLine("{0} {1}", item.latitude, item.longitude);
}
}
jsonVariables class is:
public class jsonVariables
{
[JsonProperty("latitude")]
public string latitude { get; set; }
[JsonProperty("longitude")]
public string longitude { get; set; }
[JsonProperty("state")]
public string state { get; set; }
}
In this code you access to root directory project:
string _filePath = Path.GetDirectoryName(System.AppDomain.CurrentDomain.BaseDirectory);
then:
StreamReader r = new StreamReader(_filePath + "/cities2.json"))

Add root element to json serialization in C#

I am creating a webservice to interact with a JSON API.
This API needs me to set a root element in the string, but I just cannot get this to happen.
The place where it all happens - right now just made to just show me the json output:
public static string CreateServiceChange(ServiceChange change)
{
string json = JsonConvert.SerializeObject(change);
return json;
}
This is the ServiceChange class:
public class ServiceChange
{
[JsonProperty("email")]
public string requesterEmail { get; set; }
[JsonProperty("description_html")]
public string descriptionHtml { get; set; }
[JsonProperty("subject")]
public string subject { get; set; }
[JsonProperty("change_type")]
public int changeType { get; set; }
}
And the method binding those two together:
public string copyTicketToChange(int ticketId)
{
HelpdeskTicket.TicketResponseActual ticket = getHelpdeskTicket(ticketId);
ServiceChange change = new ServiceChange();
change.descriptionHtml = ticket.Response.DescriptionHtml;
change.requesterEmail = ticket.Response.Email;
change.subject = ticket.Response.Subject;
change.changeType = 1;
string Response = Dal.CreateServiceChange(change);
return Response;
}
The json output looks like this right now:
{"email":"test#test.com","description_html":"This is a test","subject":"Testing","change_type":1}
And the expected output:
{ "itil_change": {"email":"test#test.com","description_html":"This is a test","subject":"Testing","change_type":1}}
How can I achieve this?
Wrap your ServiceChange into another object and serialize it:
public class ServiceChangeWrapper
{
public ServiceChange itil_change { get; set; }
}
...
public static string CreateServiceChange(ServiceChange change)
{
ServiceChangeWrapper wrapper = new ServiceChangeWrapper { itil_change = change};
string json = JsonConvert.SerializeObject(wrapper);
return json;
}

Categories