How to Convert Json to Object in C# - c#

I want Convert Json to Object in C#. Json here is:
[{"value":"e920ce0f-e3f5-4c6f-8e3d-d2fbc51990e4"}].
How to do it using Object.
Question seems silly but it is not so stupid.
I have not simple Json, I have IEnumerable and I am getting json from usint JsonResult like this:
new JsonResult(from c in User.Claims where (c.Type.Equals("value")) select new { c.Value });
This linq code does not work on JObject.
Thanks to Tommaso Belluzzo.

Using NewtonSoft Json.NET library (https://www.newtonsoft.com/json), you can do as follows:
JObject result = JObject.Parse(jsonString);
But your Json string looks more like an array, so probably JArray.Parse is what you need to use instead. Documentation with examples here:
https://www.newtonsoft.com/json/help/html/ParseJsonArray.htm
If you want to parse the internal elements as objects, thhe accepted answer of this question should provide you enough hints:
C# Parsing JSON array of objects

Related

Is there an easy way to get a Manatee.JsonObject from a JSON string?

We have a string that is JSON {"value":1} that we need to convert to a Manatee.Json.JsonValue.
Using new JsonValue(value) returns "{\"value\":1}" but we are looking for an actual JSON value of {"value":1}.
We have toyed with JsonObject, and it is possible by converting our JSON to a dictionary, but it would be nice if there is an easier way.
I think you need JsonValue.Parse().
From there you can get a JsonObject from the .Object property. JsonObject is derived from Dictionary<string, JsonValue> so you should be able to do that conversation simply.
The documentation is linked on the GitHub readme at the top.

Converting a JObject to a dynamic object

I'm using a library developed by another developer in our company. One of the calls in this library returns a JObject. What I need to do is convert this JObject to a dynamic object and return it to my caller.
I've found lots of answers to create a dynamic with NewtonSoft JSON.Net but all the answers use JsonConvert.DeserializeObject which is not applicable in my case as I already have a JsonObject in hand.
Any solutions?
Well, I don't really understand your problem, but you can convert it like this:
dynamic result = yourJobject;

Deserialize JSON Array with JSON.NET JArray

I'm trying to deserialize my JSON Array using Newtonsoft JSON.NET nugget:
Here's the code:
private List<TemplateTypesObj> getTemplateTypes(JArray array)
{
List<TemplateTypesObj> templateTypes = Newtonsoft.Json.JsonConvert.DeserializeObject<List<TemplateTypesObj>>(array);
return templateTypes;
}
The only issue is that DeserializeObject takes String, not an JArray object. I can do array.toString() but I'm not sure if that is a proper way to do that.
That's because a JArray doesn't really need deserializing. It's not a string/binary representation of an object (which is something you'd deserialize). It's already an object which represents your JSON. You can use it like an object - iterate through it, extract individual items from it.
Check out the docs at http://www.newtonsoft.com/json/help/html/t_newtonsoft_json_linq_jarray.htm - there are methods in there which I'm sure could be used to achieve the conversion you want.

Serialize an array into JSON objects using .NET

The serialization of the array returns the following JSON:
[{"Code":"AAAA","Description":"Description of AAAA"},{"Code":"BBBB","Description":"Description of BBBB"}]
My goal is to return the following JSON:
{"AAAA":{"Description":"Description of AAAA"},"BBBB":{"Description":"Description of BBBB"}}
You can achieve something simliar (not exactly the same you are expecting) if instead of serializing an array, build a temporary Dictionary and serialize it.
var dict = new Dictionary<String, YourClass>();
foreach (YourClass yourObj in listOfObjs)
{
dict[yourObj.Code] = yourObj;
}
// then serialize "dict"
You could add a rule to your JSON serializer to make it avoid serializing "code" property in YourClass, and you will end up with a JSON object exactly as you show in your example.
You'll need to either use a class that has the "AAAA" and "BBBB" properties, or you'll need to serialize a dictionary instead. As it is, you're serializing an array and getting an array.
The table on this blog post shows several search starting points.
.Net has built-in System.Web.Script.Serialization.JavaScriptSerializer, here, with examples
The .Net one doesn't specially serialize Dictionary the way you want, but some of the others do, I believe. However, I think there are reasons NOT to want a general serialization routine the way you've requested - though I can't list them certainly.

Looking to create JSON Serializer/Deserializer Class using C# 2.0

I am using c#.
Now I am looking to create a "JSONHelper" class, which will Serialize and Deserialize the JSON string data. In my current scenario, I am getting below format string from my web-service which returns JSON type.
I have got a method in my C# code which calls a web-service method, which returns the string JSON type data. See below example.
string userDetails = myWebService.GetMember(username, Password, out Result);
so userDetails value is
{"FullName":"Manoj Singh","username":"Manoj","Miles":2220,"TierStatus":"Gold","TierMiles":23230,"MilesExpiry":12223,"ExpiryDate":"31 January 2011","PersonID":232323,"AccessToken":"sfs23232s232","ActiveCardNo":"232323223"}
Now I want to write JSONHelper Class in which there will be methods to Serialize and Deserialize the string type of JSON, and will return the dictionary type of values.
Have you looked at http://json.codeplex.com/ ?
I've used it and find json.net very good. Not sure why you'd want to write your own.
You can use the JavaScriptSerializer class in the System.Web.Script.Serialization namespace:
JavaScriptSerializer jsonSerializer = new JavaScriptSerializer();
object result = jsonSerializer.DeserializeObject(jsonString);
Assuming your jsonString represents a object this should return a dictionary. But you will need to check the type carefully as there is no guarantee it won't return an IEnumerable for example if you give it a json list.
Better still you can use a strongly type deserialization:
http://pietschsoft.com/post/2008/02/NET-35-JSON-Serialization-using-the-DataContractJsonSerializer.aspx
If you know the return signature of your web method this is a far better solution.

Categories