1、代码
// See https://aka.ms/new-console-template for more information
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
namespace Application
{
class Program
{
static void Main(string[] args)
{
string jsonPath = "E:/Code/tool/monitor.json";
string jsonStr = GetJsonStr(jsonPath);
var monitor = GetJson(jsonStr);
string jsonPathWS = "E:/Code/tool/ws.json";
var wsStr = GetJsonStr(jsonPathWS, false);
var ws = GetJson(wsStr);
var points = GetListPoint(ws);
var jArray = (JArray)monitor;
SetJsonValue(jArray, points);
Console.WriteLine("Hello World!");
}
/***
* 反序列化成json 对象
*/
public static object GetJson(string jsonStr)
{
return JsonConvert.DeserializeObject(jsonStr);
}
/**
* 获取点坐标
*/
public static List GetListPoint(object o)
{
List points = new List();
JObject jObj = (JObject)o;
var fetures = jObj["features"];
foreach (var item in fetures)
{
var p = new Point();
p.X = double.Parse(item["geometry"]["x"].ToString());
p.Y = double.Parse(item["geometry"]["y"].ToString());
p.EXPNO =item["attributes"]["EXPNO"].ToString();
points.Add(p);
}
return points;
}
/**
* 设置json 并保存成新的json 文件
*/
public static string SetJsonValue(JArray jArray, List points)
{
Random random = new Random();
List<int> listIndex = new List<int>();
List