deserialize json newtsoft object - c#

I need to deserialize my JSON, which looks like this:
{
"cart.empty.title":"Il tuo carrello \u00e8 vuoto",
"returns.confirmation.status.step1.next.arbitration":"A breve riceverai un\u0027email con le istruzioni su come effettuare il reso.",
"returns.home_pickup.address.form.title":"Dove ritireremo i tuoi prodotti?"
}
I tried to do it like this:
string apiResponse = response.Content.ReadAsStringAsync().Result;
drupalRecords = JsonConvert.DeserializeObject<List<Record>>(apiResponse);
public class Record
{
public string Key { get; set; }
public string Value { get; set; }
}
but I have this error:
deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[TranslationsTools.Domain.Entities.Record]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
Do you know how it should be done?

A couple of problems:
Your Record class in no way matches the JSON you've shown. The property names in your object need to match those found in the JSON. In this case that's a bit tricky due to the dots (.) in the JSON property names, but luckly Newtonsoft provides a workaround for that - by using its JsonProperty attribute to match the C# property to the JSON property, even though the names are different.
You need to deserialise to a single object, not a List.
You need:
public class Record
{
[JsonProperty("cart.empty.title")]
public string CartEmptyTitle { get; set; }
[JsonProperty("returns.confirmation.status.step1.next.arbitration")]
public string ReturnsConfirmationStatusStep1NextArbitration { get; set; }
[JsonProperty("returns.home_pickup.address.form.title")]
public string ReturnsHomePickupAddressFormTitle { get; set; }
}
and
var drupalRecord = JsonConvert.DeserializeObject<Record>(apiResponse);
Live demo: https://dotnetfiddle.net/LrNc04
(The creation of the Record class above was the result of pasting your JSON into https://json2csharp.com/ for automatic conversion.)

Your json string defines a dictionary, not a list. If you don't want to define a class, you can simply deserialize the string into a dictionary.
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
public class Program
{
public static void Main()
{
string jsontext = "{ \"cart.empty.title\":\"Il tuo carrello \u00e8 vuoto\", \"returns.confirmation.status.step1.next.arbitration\":\"A breve riceverai un\u0027email con le istruzioni su come effettuare il reso.\", \"returns.home_pickup.address.form.title\":\"Dove ritireremo i tuoi prodotti?\" }";
var record = JsonConvert.DeserializeObject<Dictionary<string, string>>(jsontext);
foreach (var kvp in record) {
Console.WriteLine("{0} = {1}", kvp.Key, kvp.Value);
}
}
}
Demo here
You can access elements of the dictionary in the usual way:
Console.WriteLine(record["cart.empty.title"]); // prints Il tuo carrello รจ vuoto
However, I strongly recommend defining a class and using it like #ADyson suggests in their answer

Related

How to convert Prediction.Entities returned from LUISRuntimeClient in a strongly typed class

I'm testing a Luis app using Microsoft.Azure.CognitiveServices.Language.LUIS.Runtime package in VS2019.
I getting a list of entities from the prediction but it's a json with many properties. I want to code with it in elengant way, without dealing with json. Is there some class that i can convert the entity into it and work with?
No, there isn't class for us to convert the json entity into it, we need to parse the json by ourself. We can create a class which its attributes match the entity json properties, and then convert the json to the class object.
It's not difficult to parse the json to class, below is a sample for your reference:
public class User
{
public User(string json)
{
JObject jObject = JObject.Parse(json);
JToken jUser = jObject["user"];
name = (string) jUser["name"];
teamname = (string) jUser["teamname"];
email = (string) jUser["email"];
players = jUser["players"].ToArray();
}
public string name { get; set; }
public string teamname { get; set; }
public string email { get; set; }
public Array players { get; set; }
}
// Use
private void Run()
{
string json = #"{""user"":{""name"":""asdf"",""teamname"":""b"",""email"":""c"",""players"":[""1"",""2""]}}";
User user = new User(json);
Console.WriteLine("Name : " + user.name);
Console.WriteLine("Teamname : " + user.teamname);
Console.WriteLine("Email : " + user.email);
Console.WriteLine("Players:");
foreach (var player in user.players)
Console.WriteLine(player);
}
And there are many other solutions/samples to convert the json.
Is there some class that i can convert the entity into it and work
with?
Andrew, I have successfully deserialized a LUIS v2 REST endpoint JSON response into Microsoft.Bot.Builder.Luis.Models.LuisResult from the Microsoft.Bot.Builder v3 package (note could not find a compatible type in the latest v4 package), which is far more elegant than working with the raw JSON.

Cannot identify data structure from JSON to deserialize into object

Having issue with deserializing json string into object.
The main issue is that I can not identify what type of object this string represents:
string jsonDataText = #"{""sun"":""heat"", ""planet"":""rock"", ""earth"":""water"", ""galaxy"":""spiral""}";
It looks like List of KeyValuePair objects, but when I try to deserialize by using Newtonsoft.Json:
var clone = JsonConvert.DeserializeObject<List<KeyValuePair<string,string>>>(jsonDataText);
I have got an exception:
Cannot deserialize the current JSON object (e.g. {"name":"value"}) into type 'System.Collections.Generic.List`1[System.Collections.Generic.KeyValuePair`2[System.String,System.String]]' because the type requires a JSON array (e.g. [1,2,3]) to deserialize correctly.
Had also tried with Maps and string(multidimensional) arrays but got the same exception...
It looks like Dictionary< string,string > to me.
JsonConvert.DeserializeObject<Dictionary<string, string>>(jsonDataText);
With using JObject its easy to read any key/value pair from JSON.
So you no more need to identify what the type of your key/value pair in your json.
string jsonDataText = #"{""sun"":""heat"", ""planet"":""rock"", ""earth"":""water"", ""galaxy"":""spiral""}";
//Parse your json to dictionary
Dictionary<string, string> dict = JObject.Parse(jsonDataText).ToObject<Dictionary<string, string>>();
You need to add this namespace to your program => using Newtonsoft.Json.Linq;
Output:
It looks like to me as a simple class.
public class MyClass
{
[JsonProperty("sun")]
public string Sun { get; set; }
[JsonProperty("planet")]
public string Planet { get; set; }
[JsonProperty("earth")]
public string Earth { get; set; }
[JsonProperty("galaxy")]
public string Galaxy { get; set; }
}
Deserialize:
var clone = JsonConvert.DeserializeObject<MyClass>(jsonDataText);

Deserialize JSON response using Newtonsoft.Json

I have a web service which return response in JSON format as below.
{"123":{"Name":"Abcd", "Age":"30"},"231":{"Name":"xyz", "Age":"20"}, "543":{"Name":"pqr", "Age":"35"}}
I want to deserialize this response in C# and wants to display it.
How can I do with Newtonsoft.Json library.
Please help me.
I'm going to assume that "123", "231", and "543" are identifiers and not constant property names. In that case what you have is a dictionary of objects. First, define a class that maps to the object.
public class Something
{
public string Name { get; set; }
public string Age { get; set; }
}
Then deserialize into a dictionary of those objects.
var whatever = JsonConvert.DeserializeObject<Dictionary<string, Something>>(json);

Separating array and element from a JSON string

I am connecting you Google Places API to retrive results in the form of a JSON string. You can view the complete format of the string Here.
If you a look at it you will see that the actual results array starts after two elements which are html_attributions and next_page_token.
So When i try to deserialize it in this way:
var serializer = new JavaScriptSerializer();
var arr= serializer.Deserialize(result,typeof(string[]));
I get an empty array.
My question is how is there a way i can separate html_attributions and next_page_token fields and the pass the valid results array from the string to be deserialized?
I don't understand the part where you wish to seperate the html_attributions and the next_page_token.
Wouldn't it be sufficient to just deserialize the response with whatever properties that you need?
For example, you can deserialize the response to only retrieve the values that you desire;
// I represent the wrapper result
class Result
{
public List<string> html_attributions { get; set; }
public string next_page_token { get; set; }
public List<ResultItem> results { get; set; }
}
// I represent a result item
class ResultItem
{
public string id { get; set; }
public string name { get; set; }
}
// the actual deserialization
Result Deserialize(string json)
{
var serializer = new JavaScriptSerializer();
return serializer.Deserialize(json, typeof(Result));
}
Edit:
The reason that your deserialization doesn't return you a array of strings is because the response that you retrieve is infact an object and not an array, however the parameter within that object which is named results is an array. In order for you to deserialize more properties you'll have to define them in your "ResultItem" class, sorry for my poor naming here.
For instance, if you'd wish to also retrieve the icon property per result you'll have to add a property named "icon" of type string.
Meanwhile the property "photos" is an array, in order to deserialize it you'll have to create another class and add a property of type list/array of that newly created class, and it has to be named "photos" unless you use a different serializer or use DataContract and DataMember attributes (using the Name property for field mapping).
// the representation of a photo within a result item
class Photo
{
public int height { get; set; }
public List<string> html_attributions { get; set; }
public string photo_reference { get; set; }
public int width { get; set; }
}
// I represent a result item
class ResultItem
{
public string id { get; set; }
public string name { get; set; }
// the added icon
public string icon { get; set; }
// the added photos collection, could also be an array
public List<Photo> photos { get; set; }
}
Just look at the JSON result to figure out what other properties that you might want to add, for instance the "scope" property is an string whilst the "price_level" is an integer.
If I understand your comment correctly you're only interested in the actual results, you'll still have to deserialize the response correctly with its wrapper.
// the actual deserialization
List<ResultItem> Deserialize(string json)
{
var serializer = new JavaScriptSerializer();
var result = serializer.Deserialize(json, typeof(Result));
return result.results;
}
Edit2:
If you really want a string[] as a result you could simply take use of System.Linq using the code above.
string[] stringArray = result.results.Select(r => string.Format("id:{0} - name:{1}", r.id, r.name)).ToArray();
Edit3:
Instead of using the JavascriptSerializer you could use JObject functionality which can be found in the Newtonsoft.Json.Linq library.
var jsonObject = JObject.Parse(json);
string[] results = jsonObject.SelectTokens("results").Select(r => r.ToString()).ToArray();
This will give you an array of strings where each value within the array is the actual json string for each result.
If you however would like to query for the coordinates only:
var jsonObject = JObject.Parse(json);
var coordinates = jsonObject["results"]
.Select(x => x.SelectToken("geometry").SelectToken("location"))
.Select(x => string.Format("{0},{1}", (string)x.SelectToken("lat"), (string)x.SelectToken("lng")))
.ToArray();
This would give you an array of coordinates, eg:
[
"-33.867217,151.195939",
"-33.866786,151.195633",
...
]
Whatever approach you choose you'll be able to accomplish same results using either Newtonsoft or the .net serializer, whilst the Newtonsoft approach would allow you to query without creating strong types for deserialization.
I don't find the point of "[...] pass the valid results array from the string to be deserialized".
Maybe you need to switch to JSON.NET and do something like this:
// You simply deserialize the entire response to an ExpandoObject
// so you don't need a concrete type to deserialize the whole response...
dynamic responseEntity = JsonConvert.DeserializeObject<ExpandoObject>(
googlePlacesJson, new ExpandoObjectConverter()
);
// Now you can access result array as an `IEnumerable<dynamic>`...
IEnumerable<dynamic> results = responseEntity.results;
foreach(dynamic result in results)
{
// Do stuff for each result in the whole response...
}

How to deserialize JSON values in C# and ASP.NET?

I need to fetch some values obtained from an web URL.
The JSON is as given:
{"country":{"name":"India","State":"Raj": Count :239}, "Population": 25487}
Now i want to fetch the value of Count and Population using C#.
I have tried using JavaScriptSerializer(); But the problem is that its response time is much much slower.
Please suggest me a way to fetch the values from this JSON string.
Thanks
I personally use
https://github.com/ServiceStack/ServiceStack.Text
It's a very fast JSON serializer/deserializer.
I usually create an extension method to make the code tidier:
public static string ToJson(this object _obj)
{
return JsonSerializer.SerializeToString(_obj);
}
Edit:
A quick way to fetch those values would be to create a class of the data:
public class Country
{
public string name { get; set; }
public string State { get; set; }
public int Count { get; set; }
}
public class CountryInformation
{
public Country Country { get; set; }
public int Population { get; set; }
}
Then, using ServiceStack:
void SomeFunction(string _Json)
{
var FeedResult = _Json.FromJson<CountryInformation>();
}
You can then get the values from FeedResult as such:
FeedResult.Country.name;
One option is to use Json.NET - http://json.codeplex.com/
I normally recommend using typed POCO's like #misterjingo suggested.
However for one-off tasks you can use ServiceStack's Json Serializer to parse it dynamically like:
var json = "{\"country\":{\"name\":\"India\",\"State\":\"Raj\": \"Count\": 239}, \"Population\": 25487}";
var jsonObj = JsonObject.Parse(json);
var country = jsonObj.Object("country");
Console.WriteLine("Country Name: {0}, State: {1}, Count: {2} | Population: {3}",
country["name"], country["State"], country["Count"], jsonObj["Population"]);
//Outputs:
//Country Name: India, State: Raj, Count: 239 | Population: 25487
Note: the JSON spec requires an object property name to be a string which is always double quoted.
ServiceStack's JsonSerializer is also the fastest Json Serializer for .NET much faster than other Json Serializers.
You could also use the newer DataContractJsonSerializer class to work with JSON data.
You can either use DataContractSerialiser (ideally if you create the feed) and if you have a tricky malformed json string use JSON.net as it gives you linq2json to parse through it one node at a time.

Categories

Resources