-1

I have an array object which I have deserialized using Newtonsoft JSON.

Now I need to convert the deserialized object to class properties.

My deserialized object is: -

[
  [
    {
      "filterName": "Is Active",
      "filterformattedValue": "True",
      "filterValue": true,
      "view": "Demo/UsersbyFunction"
    },    
    {
      "filterName": "Sbg",
      "filterformattedValue": "PMT",
      "filterValue": "PMT",
      "view": "Demo/UsersbyFunction"
    },
    {
      "filterName": "Sbg",
      "filterformattedValue": "SPS",
      "filterValue": "SPS",
      "view": "Demo/UsersbyFunction"
    }
  ],
  [    
    {
      "filterName": "Sbg",
      "filterformattedValue": "CORP",
      "filterValue": "CORP",
      "view": "Demo/UsersbyFunction"
    },
    {
      "filterName": "Sbg",
      "filterformattedValue": "PMT",
      "filterValue": "PMT",
      "view": "Demo/UsersbyFunction"
    },
    {
      "filterName": "Sbg",
      "filterformattedValue": "SPS",
      "filterValue": "SPS",
      "view": "Demo/UsersbyFunction"
    }
  ]
]

and my class is: -

public class Filter
{
    public string filterName { get; set; }
    public string filterformattedValue { get; set; }
    public string filterValue { get; set; }
    public string view { get; set; }
}

Now I need to make an Array object of class Filter. Is there any way to do that?

Thanks in advance.

Sagar
  • 272
  • 1
  • 4
  • 13
  • Please add the code how you serialize the JSON – Sir Rufo Dec 29 '17 at 06:57
  • Check this https://stackoverflow.com/questions/22191167/convert-json-string-to-c-sharp-object-list – imanshu15 Dec 29 '17 at 06:59
  • @SlavaUtesinov The json itself is a single object (it starts with a **{**) – Sir Rufo Dec 29 '17 at 06:59
  • @SirRufo, I am getting serialized object from angular application. and I am using this code to deserialize it var arr = JsonConvert.DeserializeObject>(HttpContext.Current.Request.Params["arr"]); – Sagar Dec 29 '17 at 07:03
  • 1
    The current data is **not valid** json - you cannot deserialize it with JSON.Net – Sir Rufo Dec 29 '17 at 07:26

3 Answers3

2

Edited to the new json structure: Ok, then you have a two dimensional object:

string str = @"[  [    {      ""filterName"": ""Is Active"",      ""filterformattedValue"": ""True"",      ""filterValue"": true,      ""view"": ""Demo/UsersbyFunction""    },        {      ""filterName"": ""Sbg"",      ""filterformattedValue"": ""PMT"",      ""filterValue"": ""PMT"",      ""view"": ""Demo/UsersbyFunction""    },    {      ""filterName"": ""Sbg"",      ""filterformattedValue"": ""SPS"",      ""filterValue"": ""SPS"",      ""view"": ""Demo/UsersbyFunction""    }  ],  [        {      ""filterName"": ""Sbg"",      ""filterformattedValue"": ""CORP"",      ""filterValue"": ""CORP"",      ""view"": ""Demo/UsersbyFunction""    },    {     ""filterName"": ""Sbg"",      ""filterformattedValue"": ""PMT"",      ""filterValue"": ""PMT"",      ""view"": ""Demo/UsersbyFunction""    },    {      ""filterName"":""Sbg"",      ""filterformattedValue"": ""SPS"",      ""filterValue"": ""SPS"",      ""view"": ""Demo/UsersbyFunction""    }  ]]";
List<List<Filter>> filters = Newtonsoft.Json.JsonConvert.DeserializeObject<List<List<Filter>>>(str);

Or if you prefer with arrays:

Filter[][] filters = Newtonsoft.Json.JsonConvert.DeserializeObject<Filter[][]>(str);
Alpha75
  • 2,140
  • 1
  • 26
  • 49
  • JSON is having array of object. This will not solve my issue. In your JSON you have added one part of array. – Sagar Dec 29 '17 at 08:41
  • @Sagar Your JSON is **not valid**. Maybe you made a mistake with copy/paste, added some chars or you really get an invalid json. Please check and update the question. – Sir Rufo Dec 29 '17 at 09:10
  • @Jesus, only thing is, remove first and last outermost curly braces. You can reverify that on https://jsonlint.com/ – Sagar Dec 29 '17 at 09:19
  • I've checked before post and I've got a list of two lists of three Filter objects. How are you deserializing the json? – Alpha75 Dec 29 '17 at 10:20
  • 2
    @Sagar - this solution works using the JSON from the current version of your question, see https://dotnetfiddle.net/YnaTVr for a demo. – dbc Dec 29 '17 at 19:09
1

Your JSON structure seems to be wrong. Verify the same on https://jsonlint.com Remove the outermost curly braces and then try this

JsonConvert.DeserializeObject<IEnumerable<IEnumerable<Filter>>>(your_JSON_String)
  • You can try it with any, but invalid data will never produce a valid result – Sir Rufo Dec 29 '17 at 07:41
  • Removing the outermost curly braces from the JSON structure should fix it. @SirRufo – Tejas Hedly Dec 29 '17 at 07:47
  • Just as a guess from me: The OP added the curly braces manually to the sample data ;o) – Sir Rufo Dec 29 '17 at 07:50
  • @SirRufo, this JSON is a collection of an array of array type object. – Sagar Dec 29 '17 at 09:25
  • Corrected JSON structure. – Sagar Dec 29 '17 at 09:31
  • @Sagar What kind of output do you expect? Filter[][] (array of array of Filter) or List>? – Sir Rufo Dec 29 '17 at 09:55
  • @SirRufo, In my web application i have collection of array type of array. While posting it to web api i have converted it into string. In api i am deserializing string object to assign to my class Filter. But not getting any luck. – Sagar Dec 29 '17 at 10:13
  • @TejasHedly, your solution won't work. Object values are null. – Sagar Dec 29 '17 at 10:21
  • @Sagar, The value of filterValue in the first array has a boolean and you are trying to parse that to string. Try changing that to a string and see if it works. – Tejas Hedly Dec 29 '17 at 10:41
0

You can try with Cinchoo ETL - an open source library for parsing / creating JSON files.

using (var jr = new ChoJSONReader<Filter>("sample10.json")
    )
{
    foreach (var x in jr)
    {
        Console.WriteLine($"FilterName: {x.filterName}");
        Console.WriteLine($"FilterformattedValue: {x.filterformattedValue}");
        Console.WriteLine($"filterValue : {x.filterValue}");
        Console.WriteLine($"View: {x.view}");
    }
}

Disclosure: I'm the author of this library.

Cinchoo
  • 6,088
  • 2
  • 19
  • 34