In my desktop application i want to parse a json file to Key,Value pair List .
Where Key is unique one and Value contains another list of Key,Value Pair . The striuctre of json string is
{
"mainkey1": {
"subkey10": [
value1
],
" subkey11":[
value2
]
},
"mainkey2": {
"subkey20": [
value0
],
"subkey21": [
value1
]
},
"mainkey3": {
"subkey30": [
value0
],
"subkey31": [
value1
]
}
.
.
.
.
.
}
How can i convert this kind of json string to some .Net object of key,value strings
key=string type value List
Any idea ?
Try deserializing to:
Dictionary<string, Dictionary<string,List<object>>>
...or use something like Newtonsoft's JSON library which has a JObject class representing any sort of JSON object .
Update:
To use the much-easier JavaScriptSerializer, just do this:
var serializer = new JavaScriptSerializer();
var obj = serializer.Deserialize<Dictionary<string, Dictionary<string, List<object>>>>(json);
Related
I have a list of data and want to convert it into string format like headers and values separated. How can I achieve this in c#?
Expected Result:
dynamic data = {
"values": [
[
5658830,
"Support And Training Services Llc",
"PAM",
"FINNESAND"
],
[
5658831,
"Training Services Llc",
"Bob",
"MCCART"
]
],
"headers": [
"ID",
"ENT_COMPANY1",
"FirstName",
"LastName"
]
}
How to convert in List into above format?
Here's a quick snippet to answer your question, in my own code. You'll still have to propogate the list of objects with whatever it is you have. Just change "object" to the name of your model.
List<string> listOfStrings = new List<string>();
List<object> listOfMyModels = new List<object>();
//fill your list of objects here with whatever it is you're converting to a string
foreach(object myObject in listOfMyModels)
{
string json = JsonConvert.SerializeObject(myObject);
listOfStrings.Add(json);
}
Now you have a list of strings that represent your objects and when needed, you can use the deserialize method to read it.
I can´t find a value in a json string using json.net
I´ve tried jsonstr[0].track_numbers[0].track_number
This is my json file.
{
"0": {
"increment_id": "112",
"track_numbers": [
{
"track_number": "2223",
"title": "tit",
"carrier_code": "custom"
}
]
},
"live_shipping_status": "Delivered"
}
I want to find the Track_nummber.
dynamic jsonstr = JsonConvert.DeserializeObject(json));
var track = jsonstr[0].track_numbers[0].track_number
(donsent work)
The 0 of your json is a string key, not an index position:
dynamic obj = JsonConvert.DeserializeObject(json);
var trackNumber = obj["0"].track_numbers[0].track_number;
Note the difference in getting the first entry of track_numbers, which is an array.
I have a json string like
{
[
"EnrityList": "Attribute",
"KeyName": "AkeyName",
"Value": "Avalue"
],
[
"EnrityList": "BusinessKey",
"KeyName": "AkeyName",
"Value": "Avalue"
]
}
I have serialized and got an object array.
Could anyone help me to get the key value pair from these object array.
You can use JsonConvert from Newtonsoft.Json to deserialize json into Dictionary.
Dictionary<string, object> values =
JsonConvert.DeserializeObject<Dictionary<string, object>>(jsonstring);
First convert the above string in proper json format by using :
str=str.Replace('[', '{');
str=str.Replace(']', '}');
//replace first occurance of {
int startPos = str.IndexOf('{');
str=str.Substring(0,startPos)+" ["+str.Substring(startPos + 1);
//replace last occurance of }
int endPos = str.LastIndexOf('}');
str=str.Substring(0,endPos)+"]";
This makes the string
str = [{"EnrityList":"Attribute","KeyName":"AkeyName","Value":"Avalue"}, {"EnrityList":"BusinessKey","KeyName":"AkeyName","Value":"Avalue"} ]
Now, since you got the json string, you can easily work with it.
we can use method as given by
How can I deserialize JSON to a simple Dictionary<string,string> in ASP.NET?
foreach(KeyValuePair<string, string> entry in myDictionary)
{
// do something with entry.Value or entry.Key
}
Looking at your example, You are trying to receive a List of certain type of elements,
So First, You will need a class to represent your data type.
class MyType
{
string EnrityList;
string KeyName;
string Value;
}
Then use DesrializeObject method to store it in the variable
var values = JsonConvert.DeserializeObject<List<MyType>>(jsonstring);
Can any body tell me how I parse this type of json in windows phone 8 using Newtonsoft.Json dll?
[
[
{
"condition_id":"1",
"condition_name":"Type 1 Diebetics"
}
],
[
{
"condition_id":"2",
"condition_name":"Type 2 dypatise"
}
]
]
Thanks
You can simply do that for retrieving condition name.
JArray jsonarray = JArray.Parse("Your Json String Here");
var dict = (JArray)JsonConvert.DeserializeObject(Convert.ToString(jsonarray));
foreach (var obj in dict[0])
{
Debug.WriteLine(obj["condition_name"]);
}
Hope it will help you,
I need to create a JSON structure in the following format:
{
"test": [
{
"mode": "2",
"test": "test3"
},
{
"mode": "1",
"test": "test3"
}
]
}
So whenever a JSON structure is created, it should be appended to the test element.
So initially I will have only the value:
string json=#"{""testfun"": [ {""mode"": ""2"", ""test"": ""test3"" } ]}";
Dictionary<string, object> objtestnew2 = new JavaScriptSerializer().Deserialize<Dictionary<string, object>>(json);
So how can I append when I get the next JSON structure to the existing dictionary element?
Split your creation of the dictionary, and the deserialization into two different steps.
The first time the method is called it should see if the dictionary exists, and if it doesn't create one. If it does exist, then you can append the deserialized data to that dictionary.
You can deserialize the second dictionary then just merge it into the first one. The simplest way to do it is a simple loop:
string json=#"{""testfun"": [ {""mode"": ""2"", ""test"": ""test3"" } ]}";
Dictionary<string, object> dict = (new JavaScriptSerializer()).Deserialize<Dictionary<string, object>>(json);
string json2=#"{""testfun"": [ {""mode"": ""1"", ""test"": ""test3"" } ]}";
Dictionary<string, object> dict2 = (new JavaScriptSerializer()).Deserialize<Dictionary<string, object>>(json2);
// loop through and update/add the items to the first dictionary
foreach(var item in dict2)
dict1[item.Key] = item.Value;