1

Currently I am facing an issue to create a LINQ statement which will generate the list of objects that I want. The following section is an example of the LINQ object that i want to process.

{
  "successful": "true",
  "result": [
    [
      {
        "Param1": "A1",
        "Param2": "A2",
        "Param3": "A3",
        "Param4": "A4",
        "Param5": "1",
        "Param6": "A5",
      },
      {
        "Param1": "B1",
        "Param2": "B2",
        "Param3": "B3",
        "Param4": "B4",
        "Param5": "2",
        "Param6": "B5",
      },
      {
        "Param1": "C1",
        "Param2": "C2",
        "Param3": "C3",
        "Param4": "C4",
        "Param5": "2",
        "Param6": "C5",
      }
    ]
  ]
}

I have a custom object class as follow,

public class CContainer
{
    public string param1{ get; set; }

    public string param2{ get; set; }

    public string param3{ get; set; }
}

My end goal is to create a list of CContainer objects, that contain only the first 3 parameters (Param1, Param2, and Param3) for each item under the 'result' category. Also, I would like to select only item which its Param5 == "2". I am currently unable to do that using LINQ, please advice.

The following snippet don't work (even if I remove the 'Where' clause).

    List<CContainer> testList = new List<CContainer>();
    string responseRet = await response.Content.ReadAsStringAsync();
    JObject o = JObject.Parse(responseRet);

    testList =
     (from item in o["result"]
      where item["Param5"].Value<string>() == "2"
      select new CCOntainer
      {
          param1 = item["Param1"].Value<string>(),
          param2 = item["Param2"].Value<string>(),
          param3 = item["Param3"].Value<string>(),
      }).ToList();
4
  • 1
    Why are you not using the property attributes and JsonConvert.Deserialize<T>() method? Commented Apr 13, 2017 at 13:56
  • I am not sure how to extract only Param1, Param2, Param3, under result with certain condition using Deserialize method. Commented Apr 13, 2017 at 14:00
  • 1
    Are you aware your results property is an array with a single element... and that single element is itself an array of your CContainer object? Is that a mistake or intentional? Commented Apr 13, 2017 at 14:16
  • I just realized that it is an array with a single element. I am not familiar with JSON, and I think this format is made intentionally, and I can't change it. Thanks for pointing it out. Commented Apr 13, 2017 at 14:57

2 Answers 2

1

"result" in your json sample is an array containing one array, which contains your objects.

So either change the json so that "result" is a 1D array, or, something along the lines of:

testList =
 (from item in o["result"].FirstOrDefault()
// etc.
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, after extracting the first element, I can process it to the list I want, I guess its the design of the JSON object.
0

You are trying to do two different things at once.

  1. You are trying to deserialize JSON into objects.
  2. You are trying to filter the returned results.

I suggest you treat them as separate operations to simplify your code.

To deserialize all the objects using JsonConvert.DeserializeObject<T>() setup your classes as follows:

[JsonObject(MemberSerialization.OptIn)]
public class Result
{
    [JsonProperty("success")]
    public bool Success{ get; set; }

    [JsonProperty("result")]
    public List<List<JsonCContainer>> Items{ get; set; }

}

[JsonObject(MemberSerialization.OptIn)]
public class JsonCContainer
{
    [JsonProperty("Param1")]
    public string param1{ get; set; }

    [JsonProperty("Param2")]
    public string param2{ get; set; }

    [JsonProperty("Param3")]
    public string param3{ get; set; }

    [JsonProperty("Param5")]
    public string param5{ get; set; }

}

public class CContainer
{
    public string param1{ get; set; }
    public string param2{ get; set; }
    public string param3{ get; set; }
}

You would get your Result object with the following code:

string responseRet = await response.Content.ReadAsStringAsync();
Result resultObj = JsonConvert.DeserializeObject<Result>(responseRet);

Once you've got your Result class object, then do the filtering:

return resultObj.Items.SelectMany(x => x)
                .Where(x => x.param5 == "2")
                .Select(y => new CContainer()
                                 {
                                   param1 = y.param1, 
                                   param2 = y.param2, 
                                   param3 = y.param3
                                 }).ToList();

2 Comments

Some really nice code, but the json structure is the issue.
Updated to handle the multidimensional array.

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.