1

I need to serialize the Json to List of values and don't want to any dependant class(means get,set) because of dynamically change the Json keys and also increase the thousands of keys after execution (I have an external json file).

I searched lots of link but unable to solve the issue I got only converted dictionary type only. Sample json is below,

{"ALC":"FSC","AVS":"7","CAB":"M","CL":"W","CNF":"N","CNX":"N","DES":"DEL","DTE":"3","EDT":"29 Jun 2017 09:20","FBAG":"15 Kg","FBC":"W2IPO","FCUR":null,"FN":"9W 822","FQT":true,"FRI":"FSC0","FYT":"160","ITN":"0","JYT":"160","MCL":"0","OFF":0,"OFI":false,"OFR":null,"ORG":"MAA","PC":"9W","RBD":null,"RFN":"False","RTK":"TAX:1801ASPLIT2901ASPLIT1101ASPLITADT1ASPLIT","SDT":"29 Jun 2017 06:40","SGD":"Aircraft Type : 738\u000d\u000aJourney Time : 160\u000d\u000aStart Terminal : 1\u000d\u000aEndTerminal : 3\u000d\u000aBaggage : 15 Kg","SGR":"","STE":"1","STP":"0","TNF":false,"VIA":"","VIAITN":null}

I stuck in this issue for past one day.Pls anyone give the solutions.My code is below..

  var json = File.ReadAllText(Server.MapPath("JSON/Flight_res.txt"));
  JToken rss = JObject.Parse(json);
  var items= rss.SelectToken("FL").ToString();
  var jss = new JavaScriptSerializer();

  dynamic listofobj = jss.Deserialize<dynamic>(items.ToString());
6
  • 1
    Your data structure appears to be flat? What was wrong with getting a dictionary? Do you have duplicate keys? Commented Jun 1, 2017 at 6:15
  • No duplicate keys.but i need to List of value.becz ajax not suport dictionary of values Commented Jun 1, 2017 at 6:18
  • Are you trying to serialize (object->json) or deserialize (json->object)? Commented Jun 1, 2017 at 6:20
  • deserialize the Json string and trying convert to list Commented Jun 1, 2017 at 6:22
  • What have you tried to deserialize that into a dictionary? It should work. Commented Jun 1, 2017 at 6:24

2 Answers 2

0

JSON.net allows you to Deserialize to an Anonymous Type as described here: http://www.newtonsoft.com/json/help/html/DeserializeAnonymousType.htm

So you can use it in this way

string json = "{\"ALC\":\"FSC\",\"AVS\":\"7\",\"CAB\":\"M\",\"CL\":\"W\",\"CNF\":\"N\",\"CNX\":\"N\",\"DES\":\"DEL\",\"DTE\":\"3\",\"EDT\":\"29 Jun 2017 09:20\",\"FBAG\":\"15 Kg\",\"FBC\":\"W2IPO\",\"FCUR\":null,\"FN\":\"9W 822\",\"FQT\":true,\"FRI\":\"FSC0\",\"FYT\":\"160\",\"ITN\":\"0\",\"JYT\":\"160\",\"MCL\":\"0\",\"OFF\":0,\"OFI\":false,\"OFR\":null,\"ORG\":\"MAA\",\"PC\":\"9W\",\"RBD\":null,\"RFN\":\"False\",\"RTK\":\"TAX:1801ASPLIT2901ASPLIT1101ASPLITADT1ASPLIT\",\"SDT\":\"29 Jun 2017 06:40\",\"SGD\":\"Aircraft Type : 738\u000d\u000aJourney Time : 160\u000d\u000aStart Terminal : 1\u000d\u000aEndTerminal : 3\u000d\u000aBaggage : 15 Kg\",\"SGR\":\"\",\"STE\":\"1\",\"STP\":\"0\",\"TNF\":false,\"VIA\":\"\",\"VIAITN\":null}";

dynamic myData = JsonConvert.DeserializeAnonymousType(json, new ExpandoObject());

ExpandoObject is effectively just a special type of dictionary internally, so you can extract the data by iterating it like this;

foreach (var property in (IDictionary<string, string>)myData)
{
    Console.WriteLine(property.Key + ": " + property.Value);    
}

Or you can specify exactly what you want to extract if you know the "key" in code such as

string text = myData.ALC;

But because the ExpandoObject is just a type of dictionary, then this is no different to extracting straight to a dictionary by doing the following;

Dictionary<string, Object> myDict = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
foreach (var property in (IDictionary<String, string>)myDict)
{
    Console.WriteLine(property.Key + ": " + property.Value);
}
Sign up to request clarification or add additional context in comments.

6 Comments

@ jason.Thanks for ur response..I got Cast excepton
I tested the code and it worked on the example JSON you gave. But then maybe trying changing to <string, string>.
Its working fine in manually declare the json string.when i read the json file got the exception..
Can you provide a link to the full JSON?
how to split key and values using ExpandoObject
|
0

you can convert it into Dictionary.

using Newtonsoft.Json;

string jsonString = "{\"ALC\":\"FSC\",\"AVS\":\"7\",\"CAB\":\"M\",\"CL\":\"W\",\"CNF\":\"N\",\"CNX\":\"N\",\"DES\":\"DEL\",\"DTE\":\"3\",\"EDT\":\"29 Jun 2017 09:20\",\"FBAG\":\"15 Kg\",\"FBC\":\"W2IPO\",\"FCUR\":null,\"FN\":\"9W 822\",\"FQT\":true,\"FRI\":\"FSC0\",\"FYT\":\"160\",\"ITN\":\"0\",\"JYT\":\"160\",\"MCL\":\"0\",\"OFF\":0,\"OFI\":false,\"OFR\":null,\"ORG\":\"MAA\",\"PC\":\"9W\",\"RBD\":null,\"RFN\":\"False\",\"RTK\":\"TAX:1801ASPLIT2901ASPLIT1101ASPLITADT1ASPLIT\",\"SDT\":\"29 Jun 2017 06:40\",\"SGD\":\"Aircraft Type : 738\u000d\u000aJourney Time : 160\u000d\u000aStart Terminal : 1\u000d\u000aEndTerminal : 3\u000d\u000aBaggage : 15 Kg\",\"SGR\":\"\",\"STE\":\"1\",\"STP\":\"0\",\"TNF\":false,\"VIA\":\"\",\"VIAITN\":null}";

Dictionary<string, string> dict = new Dictionary<string, string>();

dict = JsonConvert.DeserializeObject <Dictionary<string, string>>(jsonString);
var list = dict.ToList(); 

Edit

var json = File.ReadAllText("Demo.json");

Dictionary<string, string> dict = new Dictionary<string, string>();

dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);

var list = dict.ToList();

This code working fine.

3 Comments

I need to convert List of values not a Disctionary format
json is basically a key-value pair, while the List is collection of homogeneous data(most of the time).So according to my understanding both are different data structure.So this is not logical correct to covert Json into List.
Its working fine in manually declare the json string.when i read the json file got the exception.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.