简体   繁体   中英

How to convert Json to List in c# without dependant class

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());

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);
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM