This question already has answers here:
JSON.NET JObject - how do I get value from this nested JSON structure
(1 answer)
JSon.NET deserialising subitems
(1 answer)
Json.NET get nested jToken value
(1 answer)
How to get a string value from a JToken
(4 answers)
Closed 1 year ago.
i have a json in below format
{{
"encoding_version": 1,
"root": {
"_type": "dictionary",
"test1": 0,
"test2": 6593,
"test3": ".key.test",
"test4": "key.test",
"test5": ".key.14",
"test6": 6159
}
}}
i am trying to get "test5" value which is 14
var data = (JObject)JsonConvert.DeserializeObject(inputJson);
var id = data["test5"];
however getting it null,Please help.
You have provide JSON with two times curl braces, i have made this small correction in your input JSON :
{
"encoding_version": 1,
"root": {
"_type": "dictionary",
"test1": 0,
"test2": 6593,
"test3": ".key.test",
"test4": "key.test",
"test5": ".key.14",
"test6": 6159
}
}
As your deserialization is correct and you are looking for value of "test5" which is ".key.14". Your desired key is two level down in JSON i.e. first go for "root" and then "test5". Hence your code will look like:
string test5Value = (string)result["root"]["test5"];
output => .key.14
If you want 14 then you might need to split output string and take out the required value.
Related
This question already has answers here:
How can I deserialize JSON with C#?
(19 answers)
Closed 2 years ago.
Below is my JSON information where you can notice that there is not any key defined.
{"KOA": {"test.jpg": "xyz.com/images/test.jpg"}}
I am Looking the Output like:
string _imgName= ms-koa-acazia-ceramic.jpg
string _imgUrl= xyz.com/images/test.jpg
"KOA" is also a dynamic value it can be changed so I can not fix it.
One approach you can use to achieve this is to use dictionaries to deserialize the JSON object
const string jsonString = "{\"KOA\": { \"test.jpg\": \"xyz.com / images / test.jpg\"}}";
var jsonObject = JsonConvert.DeserializeObject<IDictionary<string, IDictionary<string, string>>>(jsonString);
string firstKey = jsonObject.Keys.First();
string _imgName = jsonObject[firstKey].Keys.First();
string _imgUrl = jsonObject[firstKey].Values.First();
This question already has answers here:
How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?
(22 answers)
Closed 3 years ago.
I have a JSON similar to the following:
{
"type": "FeatureCollection",
"totalFeatures": "unknown",
"features": [
{
"type": "Feature",
"id": "xxx",
"geometry": {
"type": "MultiPolygon",
"coordinates": [
[
570389.865,
4722149.567
],
[
570389.865,
4722149.567
]
]
}
}
]
}
Is there a way to get the coordinates property of the first feature without using substring or parsing it to an class that represents that JSON?
I'm looking for something standard for handling JSON strings as an Object, with methods for getting childs by name or similar.
Any help would be appreciated
You can use Newtonsoft.Json library.
Here's example of getting coordinates field (using JSONPath):
var parsed = JObject.Parse(yourJson);
// returns JToken (but actually it's JArray, derived from JToken)
var coordArrayToken = parsed.SelectToken("$.features[0].geometry.coordinates");
var coordinates = coordArrayToken.ToObject<decimal[][]>();
Of course you can use simple indexers:
var parsed = JObject.Parse(yourJson);
// returns JToken (but actually it's JArray, derived from JToken)
var coordArrayToken = parsed["features"].Children().First()["geometry"]["coordinates"];
var coordinates = coordArrayToken.ToObject<decimal[][]>();
So i have a List of string which holds Indivdual json data, because it is in c# .netCore it already holds escape character('\') for double quotes, but in real it displays correct value that i know, but next i want an array of all these individual json result as a whole json result. what i am doing is directly Serializing this list of string as shown in below code, but it is adding extra escape charaters. is there any other better way of doing this using JsonResult or something?
string json = JsonConvert.SerializeObject(ListOfIndiviualJson);
If I understood correctly, ListOfIndiviualJson is a List<string> that contains strings like
{
"key1": "value",
...
}
And you want to serialise it into something like:
[
{
"key1": "value",
...
},
...
]
You can Parse each of the JSON strings into JObject, then serialise the List<JObject>:
JsonConvert.SerializeObject(ListOfIndiviualJson.Select(JObject.Parse).ToList());
Obviously, if ListOfIndiviualJson actually contains JSON arrays instead of JSON objects, use JArray.Parse.
If I understood you correctly you have a list of json object stored as I string, but now you want it as an array of strings.
string json = #"
[
{ ""test1"" : ""desc1"" },
{ ""test2"" : ""desc2"" },
{ ""test3"" : ""desc3"" }
]";
JArray a = JArray.Parse(json);
var _len = a.Count; // will be 3
Then you can convert it to a string and in json view you will see normal output.
This question already has answers here:
Deserializing JSON Object Array with Json.net
(6 answers)
Deserialize JSON with C#
(10 answers)
Closed 6 years ago.
i have a json object that has a json array that i would love to deserialize
{
"status": 1,
"loans": [
{
"loan_id": "686",
"client_id": "269",
"client_name": "Byaruhanga Guard",
"total_amount": "10000",
"min_amount": "10000",
"start_date": "2016-07-28 13:12:30",
"duration": "2",
"payment_interval": "day",
"voucher": "92",
"description": "FUEL",
"when_added": "2016-07-28 13:12:30",
"approval_status": "approved",
"total_paid": null,
"next_payment_date": "2016-07-29 10:12:30"
}
]
}
please help.
use Newtonsoft.Json from NuGet package
var results= JsonConvert.DeserializeObject<string[]>(jsondata);
var loanjson=results["loans"];
var loandata= JsonConvert.DeserializeObject<string[]>(loanjson);
This question already has answers here:
object to deserialize has a C# keyword
(4 answers)
Closed 7 years ago.
I will try to parse the string of JSON but there is a problem to parse 'long' character in JSON string.
JSON code is below
{ "valid": 1, "delta": 0, "time": 23755, "date": 200815, "fix": 2, "status": 1, "sats": 12, "lat": 37529922, "long": 126898053, "speed": 874, "heading": 0, "alt": 171300 }
I want to get lat and long value but I can't get value because Long character is keyword
My code is below
using (WebClient wc = new WebClient())
{
string json = wc.DownloadString(sb.ToString());
dynamic temp = JsonConvert.DeserializeObject(json);
Gps = new GpsInfo();
Gps.latY = temp.lat;
Gps.lonX = temp.long; //Error long type is keyword
SettingGpsChart(Gps);
}
How can I parse json value long and lat?
Use # symbol:
Gps.lonX = temp.#long;
And cast to your type:
Gps.lonX = (long)temp.#long;