在 c# asp.net 中基于 json 数据更新逻辑

Update logic based on json data in c# asp.net

提问人:hud 提问时间:10/14/2023 更新时间:10/14/2023 访问量:38

问:

我有一个示例数据,如下所示。我想用下面的JSON做的是。下面的 IF 响应有 2 件事,例如JSON

如果是,则successtrueParentList.length > 0(logic of updation in C#)

{
    "success": "true",
    "Building_rjid": "NVMBBD0015609",
    "ParentList": [
        {
            "Site_sap_id": "I-MU-MUMB-ENB-I134",
            "Site_GeoCoordinates": "19.01612,73.04158",
            "Site_priority": "P1",
            "BearingAngleFromBuilding": 278
        },
        {
            "Site_sap_id": "I-MU-MUMB-ENB-7172",
            "Site_GeoCoordinates": "19.0138,73.041",
            "Site_priority": "P2",
            "BearingAngleFromBuilding": 205
        },
        {
            "Site_sap_id": "I-MU-MUMB-ENB-7202",
            "Site_GeoCoordinates": "19.01841417,73.043785",
            "Site_priority": "P3",
            "BearingAngleFromBuilding": 33
        }
    ]
}

更新

这是我如何带来JSON响应

try
        {
            strInputJSON_BldgRJID = (new JavaScriptSerializer()).Serialize(inputBldgRJID);

            WebClient client = new WebClient();
            client.Headers["Content-type"] = "application/json";
            client.Encoding = Encoding.UTF8;
            jsonData = client.UploadString(apiUrl_CheckBldg, strInputJSON_BldgRJID); // json response
            
        }
JavaScript C# asp.net JSON

评论


答:

1赞 vdschuck 10/14/2023 #1

据我所知,你想做这样的事情:

public class ModelX
{
    [JsonPropertyName("success")]
    public bool Success { get; set; }

    [JsonPropertyName("Building_rjid")]
    public bool BuildingRjId { get; set; }

    [JsonPropertyName("ParentList")]
    public List<ParentList> ParentList { get; set; }

}
public class ParentList
{
    // TODO
}
public class Test
{
    static void Main(string[] args)
    {
        var jsonString = "{\r\n    \"success\": \"true\",\r\n    \"Building_rjid\": \"NVMBBD0015609\",\r\n    \"ParentList\": [\r\n        {\r\n            \"Site_sap_id\": \"I-MU-MUMB-ENB-I134\",\r\n            \"Site_GeoCoordinates\": \"19.01612,73.04158\",\r\n            \"Site_priority\": \"P1\",\r\n            \"BearingAngleFromBuilding\": 278\r\n        },\r\n        {\r\n            \"Site_sap_id\": \"I-MU-MUMB-ENB-7172\",\r\n            \"Site_GeoCoordinates\": \"19.0138,73.041\",\r\n            \"Site_priority\": \"P2\",\r\n            \"BearingAngleFromBuilding\": 205\r\n        },\r\n        {\r\n            \"Site_sap_id\": \"I-MU-MUMB-ENB-7202\",\r\n            \"Site_GeoCoordinates\": \"19.01841417,73.043785\",\r\n            \"Site_priority\": \"P3\",\r\n            \"BearingAngleFromBuilding\": 33\r\n        }\r\n    ]\r\n}";
        
        var model = JsonSerializer.Deserialize<ModelX>(jsonString);

        if (model != null && model.Success && model.ParentList.Count > 0)
        {
            // TODO
        }
    }
}

评论

0赞 hud 10/14/2023
我遇到错误,因为找不到(您是否错过了使用......JsonPropertyName
0赞 vdschuck 10/14/2023
是的,Visual Studio 会自动导入此“使用 System.Text.Json.Serialization;”。对于 JsonSerializer,您需要导入“using System.Text.Json;”。您使用的是哪个版本的 dotnet?
0赞 hud 10/14/2023
我正在使用 VS 2013
0赞 hud 10/16/2023
谢谢VDSCHUCK,这里需要更多帮助 stackoverflow.com/questions/77297760/...