Download values of list from json file with 2 objects - c#

I want to create simple game and i need to get values from json or change them.
I have 3 classes.
public class Person
{
public string Specjality { get; set; }
public int PWZ { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public List<string> Items { get; set; }
}
public class Monster
{
public string Name { get; set; }
public List<string> Skills { get; set; }
}
public class Root
{
public List<Person> people { get; set; }
public List<Monster> monsters { get; set; }
}
Json file
{
"Person": [
{
"Speciality": "Archer",
"PWZ": 432742,
"Name": "Charlie",
"Surname": "Evans",
"Items": [
"Bow",
"Arrow",
]
},
{
"Speciality": "Soldier",
"PWZ": 432534879,
"Name": "Harry",
"Surname": "Thomas",
"Items": [
"Gun",
"Knife",
]
}
],
"Monster": [
{
"Name": "Papua",
"Skills": [
"Jump",
"SlowWalk",
]
},
{
"Name": "Geot",
"Skills": [
"Run",
"Push",
]
}
]
}
My first problem is how to deserialize it, next how to print chosen values (for examples Skills of first Monster) on the screen and how to change some values (for example change "Jump" from Monster Skills to "Teleport" or change Name of Person). Thanks for all answers.

You're very close. To deserialize update the following:
Update/fix spelling/typo in Person class
public string Speciality { get; set; }
Update Root property names (the names should match the Json):
public class Root
{
public List<Person> Person { get; set; }
public List<Monster> Monster { get; set; }
}
Deserialize to Root class:
var result = JsonSerializer.Deserialize<Root>(json);
Now, there's also an issue with trailing commas in your Json string. I got this error:
The JSON array contains a trailing comma at the end which is not
supported in this mode. Change the reader options.
You can fix the Json string (remove the trailing commas in the arrays), or if you're using System.Text.Json to deserialize you can apply the AllowTrailingCommas option:
var options = new JsonSerializerOptions { AllowTrailingCommas = true };
var result = JsonSerializer.Deserialize<Root>(json, options);
To display the values:
// iterate over Person/Monster collection
foreach (var person in result.Person)
Console.WriteLine($"Name: {person.Name}, Items: {string.Join(", ", person.Items)}");
foreach (var monster in result.Monster)
Console.WriteLine($"Name: {monster.Name}, Skills: {string.Join(", ", monster.Skills)}");
To modify an item you would just modify the Person or Monster instance, then serialize again:
// update name:
result.Person[0].Name = "Alan";
// serialize if you need to:
string json = JsonSerializer.Serialize(result);

Related

Convert custome Key+Value JSON object Response to C# Model

i have this API that i want to creat a dynamic object. My point here is to have normal object to deal with rather than what this API returns to me, i can't controll this API so the retrived data can't be modified, this is the body, and it represnet a view in Database
Also i think reflection could help me in this case, any idea....
Request Body:
{
"ViewName": "Person",
"ViewOutput": "Name, Email, Number",
"ViewFilter": [
{
"filterKey": "Number",
"filterValue": "532000000"
}
]
}
I want the ViewName + ViewOutput+ ViewFilter to be paramterized,
ViewName it will take single value,
ViewOutput will be array of string
ViewFilter will be list of filteration ("FilterKey", "FilterVlaue") because it could be mutliple filteration value like this:
"ViewFilter": [
{
"filterKey": "Number",
"filterValue": "532000000"
},
{
"filterKey": "Email",
"filterValue": "test1#test.ps"
}
]
This is What API Return to me, a list of Person Keys and values,
Response:
{
"ResponseCode": "0",
"ResponseMessage": "Success",
"NumberOfRecords": "1",
"MainData": [
{
"recordData": [
{
"dataKey": "Name",
"dataValue": "Test Name"
},
{
"dataKey": "Email",
"dataValue": "test#test.ps"
},
{
"dataKey": "Number",
"dataValue": "532000000"
}
]
}
]
}
What i want to be the output is like this:
"Person": [
{
"Name":"Test",
"Email":"test#test.ps",
"Number":"532000000",
}]
Pasting the response JSON into an empty class file in Visual Studio using the Paste JSON As Classes feature, yields these classes:
public class Rootobject
{
public string ResponseCode { get; set; }
public string ResponseMessage { get; set; }
public string NumberOfRecords { get; set; }
public Maindata[] MainData { get; set; }
}
public class Maindata
{
public Recorddata[] recordData { get; set; }
}
public class Recorddata
{
public string dataKey { get; set; }
public string dataValue { get; set; }
}
You can create your own Person class.
Once you deserialize the JSON into the generated classes, one way to get a List<Person> would be to do something like this (please note that this is untested example code):
var people = rootObject.Maindata.Select(r =>
{
var name = r.recordData.Single(d => d.dataKey == "Name").dataValue;
var email = r.recordData.Single(d => d.dataKey == "Email").dataValue;
var number = r.recordData.Single(d => d.dataKey == "Number").dataValue;
return new Person(name, email, number);
}).ToList();

Retrieve JSON object till level 1

I'm reading data from database and converting it to a JSON object which have multiple collections and multiple levels but I only want to retrieve json till level 1 and no related collections or data. I've used following line of code in C# for that purpose
var json = JsonConvert.SerializeObject(obj, new JsonSerializerSettings() { MaxDepth = 1, ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
Above line is returning following
{
"abc": [
{
"Id": "TEST-06",
"No": 1,
"Code": " ",
"Description": ".....",
"Percentage": null,
"Details": []
}
],
"xyz": {
"Id": "TEST-06",
"No.": 1,
"Date": "2018/07/06",
"Enable": 0,
"Reason": "....."
}
}
I only want the object "abc" but it's also returning xyz.
I searched everywhere and they say use max depth property to serialize json object to specific depth. Can anyone please help with that?
You can get only "abc" object simply by using JObject property:
var jObject = JObject.Parse(YourJSonString);
var abcObject = jObject.First;
Update for Without "abc" Object
Create a class with any name for the JSon main object.
public class AnyClass
{
public string Id { get; set; }
public long No { get; set; }
public string Code { get; set; }
public string Description { get; set; }
public object Percentage { get; set; }
public List<object> Details { get; set; }
public List<object> xyz { get; set; }
}
Deserialize your JSon string with this class type:
var deserializeObject = JsonConvert.DeserializeObject<AnyClass>(jsonString);
Now in deserializeObject, you would have all properties and xyz object.

How can I deserialize a nested object in JSON.NET for FormUrlEncodedContent? [duplicate]

This question already has answers here:
c# Build URL encoded query from model object for HttpClient
(4 answers)
Closed 3 years ago.
The API I am working with requires JSON data, but also requires that the data be sent in the Content-Type of `application/x-www-form-urlencoded' so I am deserializing to a dictionary to pass the body to FormUrlEncodedContent. Everything was reasonably well with the world until I had to send a body that wasn't a flat structure.
I was reading the answer here, and thought that I had stumbled upon the solution to my problem, but I'm getting the following error:
Error reading string. Unexpected token: StartObject. Path 'filters[0]', line 1, position 165.
I am reasonably sure before I tried this method, the position was position 164, so at least I've made some progress before falling on my hands and knees here.
Here's an example of what I am doing.
Objects:
public partial class SearchRequest
{
[JsonProperty("id")]
public string Id { get; set; }
JsonProperty("fieldId")]
public string FieldId { get; set; }
[JsonProperty("fields")]
public string[] Fields { get; set; }
[JsonProperty("filters")]
public Filter[] Filters { get; set; }
[JsonProperty("pageNumber")]
public long PageNumber { get; set; }
[JsonProperty("searchText")]
public string SearchText { get; set; }
[JsonProperty("sorting")]
public Sorting[] Sorting { get; set; }
[JsonProperty("promptValues")]
public PromptValue[] PromptValues { get; set; }
}
public class Filter
{
[JsonProperty("fieldId")]
public string FieldId { get; set; }
[JsonProperty("operator")]
public string Operator { get; set; }
[JsonProperty("value")]
public string Value { get; set; }
}
public class PromptValue
{
[JsonProperty("id")]
public string Id { get; set; }
[JsonProperty("collectionValue")]
public string CollectionValue { get; set; }
[JsonProperty("promptId")]
public string PromptId { get; set; }
[JsonProperty("value")]
public Value Value { get; set; }
}
public class Value
{
}
public class Sorting
{
[JsonProperty("fieldId")]
public string FieldId { get; set; }
[JsonProperty("direction")]
public long Direction { get; set; }
}
Method:
Dictionary<string, string> dictBody;
SearchRequest searchRequest = new SearchRequest
{
// Do stuff
}
string body = Serialize.ToJson(searchRequest);
var jObj = JObject.Parse(body);
jObj["filters"] = JsonConvert.SerializeObject(jObj["filters"].ToObject<string[]>()); // Problem encountered here.
jObj["sorting"] = JsonConvert.SerializeObject(jObj["sorting"].ToObject<string[]>());
jObj["promptValues"] = JsonConvert.SerializeObject(jObj["promptValues"].ToObject<string[]>());
dictBody = JsonConvert.DeserializeObject<Dictionary<string, string>>(jObj.ToString());
JSON Sample:
{
"id": "blah",
"fieldId": "blah",
"fields": [
"blah"
],
"filters": [
{
"fieldId": "blah",
"operator": "eq",
"value": "blah"
},
{
"fieldId": "blah",
"operator": "eq",
"value": "blah"
}
],
"pageNumber": "blah",
"searchText": "blah",
"sorting": [
{
"fieldId": "blah",
"direction": "eq"
}
],
"promptValues": [
{
"id": "blah",
"collectionValue": "blah",
"promptId": "blah",
"value": {}
}
]
}
Can someone help? Even better, is there way of automatically recognising the embedded objects and flattening them?
I've tested your case and came up with the following solution:
The code below should come after your SearchRequest class.
SearchRequest searchRequest = new SearchRequest
{
// Do stuff
}
When you have your class, you do the follwing:
Dictionary<string, string> dictBody = new Dictionary<string, string>();
var properties = searchRequest.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
foreach (var prop in properties)
{
var valueAsObj = prop.GetValue(searchRequest);
var valueAsString = JsonConvert.SerializeObject(valueAsObj);
dictBody.Add(prop.Name, valueAsString);
}
This will fill your Dictionary<string, string>. Here is an example:
Hope this helps,
Cheers :)

Deserialise JSON array object with nested list in C#

I am trying to deserialise the live chat api json response to access the message id and text by filtering using user_type
JSON response
{{
"events": [
{
"type": "agent_details",
"message_id": 1,
"timestamp": 1532396384,
"user_type": "agent",
"agent": {
"name": "Adam Harris",
"job_title": "Support Agent",
"avatar": "livechat.s3.amazonaws.com/default/avatars/ab5b0666feffd67600206cd519fd77ea.jpg"
}
},
{
"type": "message",
"message_id": 3,
"timestamp": 1532396387,
"user_type": "visitor",
"text": "hi"
}
]
}}
JsonOject Class
class JsonLiveChatEvent
{
public class Rootobject
{
public Event[] events { get; set; }
}
public class Event
{
public string type { get; set; }
public int message_id { get; set; }
public int timestamp { get; set; }
public string user_type { get; set; }
public Agent agent { get; set; }
public string text { get; set; }
}
public class Agent
{
public string name { get; set; }
public string job_title { get; set; }
public string avatar { get; set; }
}
}
JsonConverter
string jsonStr= await Api.Chat.GetPendingMessages(visitorID, licenseID,
var chatEvent = JsonConvert.DeserializeObject<Rootobject>(jsonStr);
The chatEvent object will not let me call chatEvent.events.message_id for example. Any help would be greatly appreciated as this is my first time working with json in c#
There is nothing to do with JSON, you have parsed the JSON data back to Rootobject.
Now you are working with an instance of Rootobject as:
Rootobject chatEvent = JsonConvert.DeserializeObject<Rootobject>(jsonStr);
Event event1 = chatEvent.events[0];
Event event2 = chatEvent.events[1];
Also, consider the answer from Mohammad, because above JSON will throw an exception.
The main problem here is that your json is not valid, there is an extra { in the beginning and an extra } in the end.
Then you could deserialize your json with the types you provided
Your json contains more that one curly brackets so you have to first remove those
so your json look like
{
"events": [
{
"type": "agent_details",
"message_id": 1,
"timestamp": 1532396384,
"user_type": "agent",
"agent": {
"name": "Adam Harris",
"job_title": "Support Agent",
"avatar": "livechat.s3.amazonaws.com/default/avatars/ab5b0666feffd67600206cd519fd77ea.jpg"
}
},
{
"type": "message",
"message_id": 3,
"timestamp": 1532396387,
"user_type": "visitor",
"text": "hi"
}
]
}
After that you have to collect message ids depending upon user_type
So then we create enum for that
public enum UserType
{
agent, visitor
}
then we simply check in events that if user type is matches with any of above enum value.
If your json contains multiple events with multiple user types then collect those into List<int>.
If your json contains only single event of each user type then collect them into string variables.
Rootobject chatEvent = JsonConvert.DeserializeObject<Rootobject>(jsonStr);
List<int> agent_message_ids = new List<int>();
List<int> visitior_message_ids = new List<int>();
//string agent_message_id = string.Empty;
//string visitior_message_id = string.Empty;
foreach (Event e in chatEvent.events)
{
if (e.user_type == UserType.agent.ToString())
{
agent_message_ids.Add(e.message_id);
//agent_message_id = e.message_id;
}
if (e.user_type == UserType.visitor.ToString())
{
visitior_message_ids.Add(e.message_id);
//visitior_message_id = e.message_id;
}
}
We simply take a list of integers that store message ids for particular user_type
Try once may it help you
Result:
agent_message_ids:
visitor_message_ids:

How can I use ReadAsAsync<T> with this data schema?

I am using System.Net.Http.HttpClient, the version currently available in NuGet,
to retrieve data from a service in json format. The data roughly looks like this:
{
"schema": "Listing",
"data": {
"key": "28ba648c-de24-45d4-a7d9-70f810cf5438",
"children": [{
"kind": "type1",
"data": {
"body": "Four score and seven years ago...",
"parent_id": "2qh3l",
"report_count": 0,
"name": "c4j6yeh"
}
}, {
"kind": "type3",
"data": {
"domain": "abc.def.com",
"flagged": true,
"category": "news",
"saved": false,
"id": "t3dz0",
"created": 1335998011.0
}
}]
}
}
I use HttpContentExtensions.ReadAsAsync<T> to de-serialize that json string into an object graph. The type definitions looks roughly like this:
public class Response
{
public String schema { get;set; }
public ListingData data { get;set; }
}
public class ListingData
{
public string key { get;set; }
public List<OneItem> children { get;set; }
}
Here's the problem: I desire the type of the items in children to vary depending on the kind property. If kind is "type1" then I want to de-serialize an object of... let's call it Type1 . If kind is "type3" then I want an object of type Type3.
Right now, I can deserialize a List<Type1> or a List<Type3>, but I don't know how to tell the de-serialization logic to distinguish between the two.
I could merge all the properties of the "type1" data object and the "type3" data object into a single .NET Type. But the number of properties is large enough that this gets messy.
If the name of the property in the JSON (in this case data) were different, I could distinguish using that. If, for example, the data looked like this:
"children": [{
"kind": "type1",
"t1data": { ... }
}, {
"kind": "type3",
"t3data": { ... }
}]
...then I could do something like this in .NET:
public class OneItem
{
public string kind { get;set; }
public Type1 t1data { get;set; }
public Type3 t3data { get;set; }
}
But my data schema doesn't look like that.
Is it possible to choose the type for de-serialization by the content of the data? In other words,
look at the value of one property (in this case, kind) to determine how to de-serialize the content for another property (in this case, data).
Or is it possible to inject a filter or transformer that acts on the JSON before ReadAsAsync tries to deserialize it?
If so, How?
If you're ok w/ doing some pre-processing on your response and you can use Json.NET, you should be able to do what you want.
Given the following classes:
public class Response
{
public string schema
{
get;
set;
}
public ListingData data
{
get;
set;
}
}
public class ListingData
{
public string key
{
get;
set;
}
public List<object> children
{
get;
set;
}
}
public class Type1
{
public string body
{
get;
set;
}
public string parent_id
{
get;
set;
}
public int report_count
{
get;
set;
}
public string name
{
get;
set;
}
}
public class Type3
{
public string domain
{
get;
set;
}
public bool flagged
{
get;
set;
}
public string category
{
get;
set;
}
public bool saved
{
get;
set;
}
public string id
{
get;
set;
}
public double created
{
get;
set;
}
}
This test passes:
[Test]
public void RoundTrip()
{
var response = new Response
{
schema = "Listing",
data = new ListingData
{
key = "28ba648c-de24-45d4-a7d9-70f810cf5438",
children = new List<object>
{
new Type1
{
body = "Four score and seven years ago...",
parent_id = "2qh3l",
report_count = 0,
name = "c4j6yeh"
},
new Type3
{
domain = "abc.def.com",
flagged = true,
category = "news",
saved = false,
id = "t3dz0",
created = 1335998011.0
}
}
}
};
var jsonSerializerSettings = new JsonSerializerSettings
{
Formatting = Formatting.Indented,
TypeNameHandling = TypeNameHandling.Objects
};
string serializedResponse = JsonConvert.SerializeObject(response, jsonSerializerSettings);
Console.WriteLine(serializedResponse);
var roundTrippedResponse = JsonConvert.DeserializeObject<Response>(serializedResponse, jsonSerializerSettings);
Assert.That(roundTrippedResponse.data.children.First().GetType(), Is.EqualTo(typeof(Type1)));
Assert.That(roundTrippedResponse.data.children.Last().GetType(), Is.EqualTo(typeof(Type3)));
}
The output written to the console is:
{
"$type": "Test.Response, Test",
"schema": "Listing",
"data": {
"$type": "Test.ListingData, Test",
"key": "28ba648c-de24-45d4-a7d9-70f810cf5438",
"children": [
{
"$type": "Test.Type1, Test",
"body": "Four score and seven years ago...",
"parent_id": "2qh3l",
"report_count": 0,
"name": "c4j6yeh"
},
{
"$type": "Test.Type3, Test",
"domain": "abc.def.com",
"flagged": true,
"category": "news",
"saved": false,
"id": "t3dz0",
"created": 1335998011.0
}
]
}
}
So if you can transform your received response to match that of Json.NET's expected format, this will work.
To piece all of this together, you would need to write a custom MediaTypeFormatter and pass it to the ReadAsAsync<>() call.

Categories

Resources