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



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

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

我使用过JSON验证器,之后,我明白我必须这样做。如何在 Asp.net 核心中转换上述字符串 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);
}
});
}
}

困难似乎在于您的字符串既是 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

最新更新