Extract and compare only jarray value from Dictionary<string, object> param data

Viewed 87

Once again I have edited the code and question

This code is the logic to receive response data through the full request message.

I am making an api server

I want to extract only the data I want from the response data and put it back in the list

this is the code i made

public List<api_model> getList(Dictionary<string, object> param)
{
   List<api_model> tempList = new List<api_model>();

   List<api_model> resultList  = new List<api_model>();
            
   JsonRes.GetListRes res;
 try
{
  GetListRequest jsonReq = new GetListRequest();

  jsonReq.Session = param;

  string strJsonReq = jsonReq.TransformText();

  string strUri = String.Format("'API ADRESS'");

  string strJsonRes = httpUtils.HttpRequest(strUri, "POST", strJsonReq).ToString();

   using (JsonTextReader reader = new JsonTextReader(new System.IO.StringReader(strJsonRes)))
{
   JsonSerializer serializer = new JsonSerializer();
   res = (JsonRes.GetListRes)serializer.Deserialize(reader, typeof(JsonRes.GetListRes));
}
  if (res.outputCode.Equals("1"))
{
  foreach (JsonRes.GetListRes.Data data in res.dataList)
{
  List<api_model.array> arrayList = new List<api_model.array>();

  foreach (JsonRes.GetListRes.array ar in data.arrayList)
{

   api_model.array arrays = api_model api_model.array
  {
             foodCode = ar.foodCode,
             foodCount = ar.foodCount
  };
                            
         arrayList.Add(arrays);
  }
                        
         api_model um = new api_model
         {
              foodID = data.foodID,
              food = arrayList
         };
                        
              tempList.Add(bm);
          }
                    
             resultList  = tempList;
                     
                }
            }
 catch (Exception ex)
{
  CommonUtils.errorLog(ex.ToString());
  throw ex;
}

   return resultList ;
}

I want to compare the full request and response data with linq and put only the desired data in the list again

full text of request

{
  "Type": "restaurant",
  "food": [
    {
      "foodID": "chicken",
      "foodCODE": "001",
      "foodVALUE": "1"
    },
    {
      "foodID": "lamb",
      "foodCODE": "002",
      "foodVALUE": "1"
    },
    {
      "foodID": "pork",
      "foodCODE": "003",
      "foodVALUE": "1"
    },
    {
      "foodID": "eggs",
      "foodCODE": "004",
      "foodVALUE": "1"
    }
  ]
}

response data

{
  "Code": "00",
  "Msg": "SUCCESS",
  "Data": [
{
  "foodID": "123456",
  "food": [
    {
        "foodCode": "001",
        "foodCount": "1"
    },
    {
        "foodCode": "002",
        "foodCount": "1"
    },
    {
        "foodCode": "003",
        "foodCount": "1"
    },
    {
        "foodCode": "004",
        "foodCount": "1"
    },
    {
        "foodCode": "005",
        "foodCount": "1"
    }
  ]
},
{
 "foodID": "56789",
  "food": [
    {
        "foodCode": "001",
        "foodCount": "1"
    },
    {request
        "foodCode": "002",
        "foodCount": "1"
    },
    {
        "foodCode": "003",
        "foodCount": "1"
    },
    {
        "foodCode": "004",
        "foodCount": "0"
    },
    {
        "foodCode": "005",
        "foodCount": "1"
    }
  ]
}
 ]
}

I want to extract only the same data by comparing foodVALUE data in the full request and foodCount to response data

"foodID": "123456",
  "food": [
    {
        "foodCode": "001",
        "foodCount": "1"
    },
    {
        "foodCode": "002",
        "foodCount": "1"
    },
    {
        "foodCode": "003",
        "foodCount": "1"
    },
    {
        "foodCode": "004",
        "foodCount": "1"
    },
    {
        "foodCode": "005",
        "foodCount": "1"
    }
  ]

I want to extract only this data and put it in resultList

1 Answers

In the class GetListRes.Data, the property arrayList is tagged with the json property array :

public class GetListRes
{
    ...
    [JsonProperty("data")]
    public List<Data> dataList { get; set; } = new List<Data>();

    public class Data
    {

        [JsonProperty("foodID")]
        public string foodID { get; set; } = "";

        [JsonProperty("array")]
        public List<array> arrayList { get; set; } = new List<array>();
    }
}

But in the json response, the matched json property is food :

{
  "data": [
    {
      "foodID": "56789",
      "food": [
        {
          "foodCode": "001",
          "foodCount": "1"
        },
        {
          "foodCode": "002",
          "foodCount": "1"
        },
        ...
      ]
    }
  ]
}

You need to modify the code as :

public class GetListRes
{
    ...
    [JsonProperty("data")]
    public List<Data> dataList { get; set; } = new List<Data>();

    public class Data
    {

        [JsonProperty("foodID")]
        public string foodID { get; set; } = "";

        [JsonProperty("food")] // Here, replace 'array' by 'food'
        public List<array> arrayList { get; set; } = new List<array>();
    }
}

To extract the expected from the response, you can try :

var request = JsonConvert.DeserializeObject<GetListReq>(File.ReadAllText("request.json"));
var response = JsonConvert.DeserializeObject<GetListRes>(File.ReadAllText("response.json"));

var result = response.dataList
    .First(r => r.foodID == "123456")
    .arrayList.Join(request.food, r => r.foodCode, r => r.foodCode, (res, req) => new { res, req })
    .Where(d => d.req.foodValue == d.res.foodCount);
Related