根据请求从响应中提取部分Json

时间:2016-08-03 05:41:46

标签: c# json

我正在编写一个web api操作,它接受一个字符串请求(json)并返回请求json中请求的响应。

进一步阐述: 请求是:

{
    "Id":"",
   "Wells":[{"WellNumber":""}],
   "ExternalKey":""

}

我从数据库获得的数据是

[
    {
        "Id": "J.16.002219",
        "Wells": [
            {
                "WellNumber": "63000008",
                "WellName": "Well One Desc",
                "ExternalKey": "ZW",
                "Job_Id": "J.16.002219",
                "HoleSections": null
            }
        ],
        "ExternalKey": null
    }
]

我想作为web api方法的响应返回的是来自数据库的数据子集,应该看起来像

[
    {
        "Id": "J.16.002219",
        "Wells": [
            {
                "WellNumber": "63000008",                
            }
        ],
        "ExternalKey": null
    }
]

基本上我不想返回尚未请求的数据。

2 个答案:

答案 0 :(得分:0)

如果要反序列化并从数据库返回结果的子集,可以采用一种方式创建一个表示 - 并且仅包含要发回的数据的模型。 e.g

public class ResponseModel
{
   public string Id { get; set;}
   public List<Well> Wells { get; set;}
   public string ExternalKey { get; set;}
}

public class Well
{
   public string WellNumber { get; set;}
}

然后,您可以将此类型序列化为json并发回。

答案 1 :(得分:0)

你走了!收到数据库的响应后,您可以从Web API调用GetOnlyRequestedData。我已经演示了如何在main方法中调用它的示例。您必须在项目中添加对Newtonsoft.Json nuget包的引用才能使所有这些工作正常。

        static void Main(string[] args)
        {
            var inputRequest = "{\"Id\":\"\",\"Wells\":[{\"WellNumber\":\"\"}],\"ExternalKey\":\"\"}";
            var valueObtainedFromDb =
                "{\"Id\":\"J.16.002219\",\"Wells\":[{\"WellNumber\":\"63000008\",\"WellName\":\"Well One Desc\",\"ExternalKey\":\"ZW\",\"Job_Id\":\"J.16.002219\",\"HoleSections\":null}],\"ExternalKey\":null}";
            var filteredOutput = GetOnlyRequestedData(inputRequest, valueObtainedFromDb);
            Console.WriteLine(filteredOutput);
            Console.ReadLine();
        }

        private static JToken GetOnlyRequestedData(string jsonFilter, string targetData)
        {
            var jToken = JToken.Parse(jsonFilter);
            JToken outputToken = JToken.Parse(targetData);
            List<string> fieldsToBePreserved = new List<string>();
            GetFlattenedPropertyList(jToken, ref fieldsToBePreserved);
            removeFields(outputToken, fieldsToBePreserved);
            return outputToken;
        }


        private static void GetFlattenedPropertyList(JToken jObject, ref List<string> fieldsToBePreserved)
        {
            JContainer container = jObject as JContainer;
            if (container == null) return;

            foreach (JToken el in container.Children())
            {
                JProperty p = el as JProperty;
                if (p != null)
                {
                    fieldsToBePreserved.Add(p.Name + p.Path);
                }
                GetFlattenedPropertyList(el, ref fieldsToBePreserved);    
            }
        }

        private static void removeFields(JToken token, List<string> fields)
        {
            JContainer container = token as JContainer;
            if (container == null) return;

            List<JToken> removeList = new List<JToken>();
            foreach (JToken el in container.Children())
            {
                JProperty p = el as JProperty;
                if (p != null && !fields.Contains(p.Name + p.Path))
                {
                    removeList.Add(el);
                }
                removeFields(el, fields);
            }

            foreach (JToken el in removeList)
            {
                el.Remove();
            }
        }