0

i want to pass following json as an reference in my console application in c#

{"val1":["dfgdsfgdfgsdf"],"val2":258915,"val3":"PPaaaA","val4":null,

"valJSON":"[{\"TypeID\":\"Z_FI_MDG\",\"SeverityCode\":\"3\",\"Note\":\"\\\"zczczca \\\\\\\"leading zero\\\\\\\". \\\\\\\\r\\\\\\\\n•YYY: Institution\"}]"}

I am doing following , but it is not working

  JsonSerializer serializer = new JsonSerializer(); 
            dynamic item = serializer.Deserialize<object>("
{"val1":["dfgdsfgdfgsdf"],"val2":258915,"val3":"PPaaaA","val4":null,

    "valJSON":"[{\"TypeID\":\"Z_FI_MDG\",\"SeverityCode\":\"3\",\"Note\":\"\\\"zczczca \\\\\\\"leading zero\\\\\\\". \\\\\\\\r\\\\\\\\n•YYY: Institution\"}]"}

");

any other way i can pass this to function ? to simplyfy

when i try to assign this to string it gives error can anyon hlp me

3 Answers3

0

You should remove object type from function call:

  JsonSerializer serializer = new JsonSerializer(); 
  dynamic item = serializer.Deserialize("...");
eocron
  • 6,885
  • 1
  • 21
  • 50
0

I would use Newtonsoft's solution, if it's possible, i never regreted.

In your case, i'd use:

string json = @"{
  "val1": [
    "dfgdsfgdfgsdf"
  ],
  "val2": 258915,
  "val3": "PPaaaA",
  "val4": null,
  "valJSON": "[{\"TypeID\":\"Z_FI_MDG\",\"SeverityCode\":\"3\",\"Note\":\"\\\"zczczca \\\\\\\"leading zero\\\\\\\". \\\\\\\\r\\\\\\\\n•YYY: Institution\"}]"
}"

dynamic rss = JObject.Parse(json);

And then accessing values from it like:

var val2 = rss.val2;

I'm not sure if that's what you was looking for, but i tried... Read more: http://www.newtonsoft.com/json/help/html/QueryJson.htm

Then, if you want more how to "install" newtonsoft: How to install JSON.NET using NuGet?

Community
  • 1
  • 1
K4ktus
  • 1
  • 2
0

If you want to parse Json to the exact class you can try this

 RootObject item = JsonConvert.DeserializeObject<RootObject>(File.ReadAllText(@"D:\file.txt"));

 public class RootObject
    {
        public List<string> val1 { get; set; }
        public int val2 { get; set; }
        public string val3 { get; set; }
        public object val4 { get; set; }
        public string valJSON { get; set; }
    }
Justin CI
  • 2,693
  • 1
  • 16
  • 34