如何在ASP.net Core中将lat和lang的字符串类型转换为JSON?

问题描述 投票:0回答:1

在我的操作方法中,我得到了一个字符串,该字符串具有lat和lang字符串值,我将其转换为JSON。我的字符串是这样的。

"[[[{\"lat\":35.71467081484196,\"lng\":51.41189575195313},{\"lat\":35.70254403049943,\"lng\":51.45472526550293},{\"lat\":35.69292492425683,\"lng\":51.402111053466804}]]]"

我使用了JSON验证程序,然后,我知道我必须像这样做。如何在Asp.net Core中转换上述字符串JSON格式?

[
    [
        [
            {
                "lat" : 35.71467081484196,
                "lng" : 51.41189575195313
            },
            {
                "lat" : 35.70254403049943,
                "lng" : 51.45472526550293
            },
            {
                "lat" : 35.69292492425683,
                "lng" : 51.402111053466804
            }
        ]
    ]
]

    [Authorize]
    public async Task<IActionResult> Device_Setting(Guid id)
    {
        SetPanelType(PanelTypes.Device);

        var deviceSetting = await dbContext.DeviceSetting.FirstOrDefaultAsync(ds => ds.DeviceSettingId == id);
        var fence = await dbContext.DeviceFence.FirstOrDefaultAsync(d => d.DeviceSettingId == deviceSetting.DeviceSettingId);
        ViewBag.FenceArea = JsonConvert.SerializeObject(fence.ShapePath);

        return View(deviceSetting);
    }

function sendFenceData(flag) {
    if (flag) {
        var data = {
            ShapePath: JSON.stringify(ShapePath),
            ShapeType: 'Multiple'
        };
        $.ajax({
            url: '/Panel/Device_Fence',
            type: 'post',
            contentType: "application/json",
            data: data,
            processData: false,
            success: function (data, textStatus, jQxhr) {
                alert("اطلاعات با موفقیت ثبت شد.");
            },
            error: function (jqXhr, textStatus, errorThrown) {
                console.log(errorThrown);
            }
        });
    }
}
c# json asp.net-core asp.net-core-2.2
1个回答
0
投票

困难似乎是您的字符串既是html编码的,还是转义的JSON。逆转这两个过程将留下可完美解析的JSON。这是一个将值读入元组数组的示例:

using System;
using System.Net;
using System.Text.Json;
using System.Linq;

public class Program
{
    public static void Main()
    {
        const string jsonHtmlEscaped = @"&quot;[[[{\&quot;lat\&quot;:35.71467081484196,\&quot;lng\&quot;:51.41189575195313},{\&quot;lat\&quot;:35.70254403049943,\&quot;lng\&quot;:51.45472526550293},{\&quot;lat\&quot;:35.69292492425683,\&quot;lng\&quot;:51.402111053466804}]]]&quot;";
        Console.WriteLine("Raw string:");
        Console.WriteLine(jsonHtmlEscaped);
        Console.WriteLine();

        var jsonEscaped = WebUtility.HtmlDecode(jsonHtmlEscaped);
        Console.WriteLine("After Html Decode:");
        Console.WriteLine(jsonEscaped);
        Console.WriteLine();

        var json = JsonSerializer.Deserialize<string>(jsonEscaped);
        Console.WriteLine("After unescape JSON:");
        Console.WriteLine(json);
        Console.WriteLine();

        var latAndLongRaw = JsonSerializer.Deserialize<System.Text.Json.JsonElement[][][]>(json);
        var latAndLong = latAndLongRaw
            .SelectMany(a => a)
            .SelectMany(a => a)
            .Select(a => (Latitude: a.GetProperty("lat").GetDecimal(), Longitude: a.GetProperty("lng").GetDecimal()))
            .ToList();

        Console.WriteLine("After parsing:");
        foreach(var (latitude, longitude) in latAndLong)
        {
            Console.WriteLine($"Lat: {latitude}, Lon: {longitude}");
        }
    }
}

您可以在此处试用此示例:https://dotnetfiddle.net/BkIbGV

这是示例输出:

Raw string:
&quot;[[[{\&quot;lat\&quot;:35.71467081484196,\&quot;lng\&quot;:51.41189575195313},{\&quot;lat\&quot;:35.70254403049943,\&quot;lng\&quot;:51.45472526550293},{\&quot;lat\&quot;:35.69292492425683,\&quot;lng\&quot;:51.402111053466804}]]]&quot;

After Html Decode:
"[[[{\"lat\":35.71467081484196,\"lng\":51.41189575195313},{\"lat\":35.70254403049943,\"lng\":51.45472526550293},{\"lat\":35.69292492425683,\"lng\":51.402111053466804}]]]"

After unescape JSON:
[[[{"lat":35.71467081484196,"lng":51.41189575195313},{"lat":35.70254403049943,"lng":51.45472526550293},{"lat":35.69292492425683,"lng":51.402111053466804}]]]

After parsing:
Lat: 35.71467081484196, Lon: 51.41189575195313
Lat: 35.70254403049943, Lon: 51.45472526550293
Lat: 35.69292492425683, Lon: 51.402111053466804
© www.soinside.com 2019 - 2024. All rights reserved.