C# 解析嵌套的JSON并替换C中的数据#
我有一个复杂的JSON响应,需要解析和替换其中的数据 这只是从1天开始的一个小插曲,但需要在响应中执行所有天 我如何解析它并仅仅更改值。 我需要修改并发送回主机C# 解析嵌套的JSON并替换C中的数据#,c#,json,C#,Json,我有一个复杂的JSON响应,需要解析和替换其中的数据 这只是从1天开始的一个小插曲,但需要在响应中执行所有天 我如何解析它并仅仅更改值。 我需要修改并发送回主机 { "schedulePeriods":` [ { "day": "Monday", "periodType": "WakeOcc1", "startTime": 26, "isCancelled": false, "heatSetpoint": 70.0,
{
"schedulePeriods":` [
{
"day": "Monday",
"periodType": "WakeOcc1",
"startTime": 26,
"isCancelled": false,
"heatSetpoint": 70.0,
"coolSetpoint": 75.0,
"fanMode": "Auto"
},
{
"day": "Monday",
"periodType": "LeaveUnocc1",
"startTime": 32,
"isCancelled": false,
"heatSetpoint": 65.0,
"coolSetpoint": 75.0,
"fanMode": "Auto"
},
{
"day": "Monday",
"periodType": "ReturnOcc2",
"startTime": 66,
"isCancelled": false,
"heatSetpoint": 72.0,
"coolSetpoint": 75.0,
"fanMode": "Auto"
},
{
"day": "Monday",
"periodType": "SleepUnocc2",
"startTime": 0,
"isCancelled": false,
"heatSetpoint": 65.0,
"coolSetpoint": 75.0,
"fanMode": "Auto"
},
{
"day": "Tuesday",
"periodType": "WakeOcc1",
"startTime": 26,
"isCancelled": false,
"heatSetpoint": 70.0,
"coolSetpoint": 75.0,
"fanMode": "Auto"
},
所以我现在看到的是:`
class Day
{
public string day { get; set; }
public string periodType { get; set; }
public int startTime { get; set; }
public double heatSetpoint { get; set; }
public double coolSetpoint { get; set; }
public override string ToString()
{
return string.Format("{0}\n{1}\n{2}\n{3}\n{4}", day, periodType, startTime, heatSetpoint, coolSetpoint);
}
}
static void Main(string[] args)
{
String JSONstring = File.ReadAllText("json.json");
JObject o = JObject.Parse(JSONstring);
string day = (string)o.SelectToken("schedulePeriods[0].day");
string periodType = (string)o.SelectToken("schedulePeriods[0].periodType");
int startTime = (int)o.SelectToken("schedulePeriods[0].startTime");
double heatSetpoint = (double)o.SelectToken("schedulePeriods[0].heatSetpoint");
double coolSetpoint = (double)o.SelectToken("schedulePeriods[0].coolSetpoint");
Console.WriteLine("{0}\n{1}\n{2}\n{3}\n{4}",day,periodType,startTime,heatSetpoint,coolSetpoint);
Console.WriteLine("Change Value to:");
string answer = Console.ReadLine();
day = answer;
Console.WriteLine("new value {0}", day);
string data = JsonConvert.SerializeObject(JSONstring,Formatting.Indented);
File.WriteAllText("newfile.json", data);
Console.ReadLine();
}
我可以从第一个大括号中获取数据,但不能再进一步。
我需要从每个数组中获取。
我还想修改给定的条目,然后用正确的json格式保存编辑过的文件(这根本不起作用)`
解决了。
感谢您的反馈在应用程序中创建以下类
public class RootObject
{
public List<SchedulePeriod> schedulePeriods { get; set; }
}
public class SchedulePeriod
{
public string day { get; set; }
public string periodType { get; set; }
public int startTime { get; set; }
public bool isCancelled { get; set; }
public double heatSetpoint { get; set; }
public double coolSetpoint { get; set; }
public string fanMode { get; set; }
}
公共类根对象
{
公共列表schedulePeriods{get;set;}
}
公共类调度周期
{
公共字符串day{get;set;}
公共字符串periodType{get;set;}
公共int开始时间{get;set;}
已取消公共布尔值{get;set;}
公共双设定点{get;set;}
公共双精度设置点{get;set;}
公共字符串模式{get;set;}
}
然后使用Newtonsoft.Json反序列化类似Json的
public static T DeserializeObject<T>(string jsonObj)
{
return JsonConvert.DeserializeObject<T>(jsonObj,
new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore, Formatting = Formatting.Indented });
}
publicstatict反序列化对象(字符串jsonObj)
{
返回JsonConvert.DeserializeObject(jsonObj,
新的JsonSerializerSettings(){ReferenceLoopHandling=ReferenceLoopHandling.Ignore,Formatting=Formatting.Indented});
}
就这样说吧
RootObject obj = DeserializeObject<RootObject>(your_json);
rootobjectobj=反序列化对象(您的json);
然后访问您的日程安排
希望有帮助。在应用程序中创建以下类
public class RootObject
{
public List<SchedulePeriod> schedulePeriods { get; set; }
}
public class SchedulePeriod
{
public string day { get; set; }
public string periodType { get; set; }
public int startTime { get; set; }
public bool isCancelled { get; set; }
public double heatSetpoint { get; set; }
public double coolSetpoint { get; set; }
public string fanMode { get; set; }
}
公共类根对象
{
公共列表schedulePeriods{get;set;}
}
公共类调度周期
{
公共字符串day{get;set;}
公共字符串periodType{get;set;}
公共int开始时间{get;set;}
已取消公共布尔值{get;set;}
公共双设定点{get;set;}
公共双精度设置点{get;set;}
公共字符串模式{get;set;}
}
然后使用Newtonsoft.Json反序列化类似Json的
public static T DeserializeObject<T>(string jsonObj)
{
return JsonConvert.DeserializeObject<T>(jsonObj,
new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore, Formatting = Formatting.Indented });
}
publicstatict反序列化对象(字符串jsonObj)
{
返回JsonConvert.DeserializeObject(jsonObj,
新的JsonSerializerSettings(){ReferenceLoopHandling=ReferenceLoopHandling.Ignore,Formatting=Formatting.Indented});
}
就这样说吧
RootObject obj = DeserializeObject<RootObject>(your_json);
rootobjectobj=反序列化对象(您的json);
然后访问您的日程安排
希望有帮助。你能出于某种原因不使用
Newtonsoft.Json
吗?用c#创建一个模型,以匹配Json中的数据。使用类似Newtonsoft.Json的东西(如已经建议的)将传入的Json解析为对象。更改您需要的任何内容,然后再次使用它将对象转换回json。只需使用newtonsoft的库即可。创建一个与JSON模式匹配的模型,然后对该模型进行序列化或反序列化。您的模型将填充数据,“反序列化”或获取填充的模型并将其序列化回json中的字符串。-请参阅我的答案可能重复的您是否可以出于某种原因不使用Newtonsoft.Json
?用c#创建一个模型以匹配Json中的数据。使用类似Newtonsoft.Json的东西(如已经建议的)将传入的Json解析为对象。更改您需要的任何内容,然后再次使用它将对象转换回json。只需使用newtonsoft的库即可。创建一个与JSON模式匹配的模型,然后对该模型进行序列化或反序列化。您的模型将填充数据,“反序列化”或获取填充的模型并将其序列化回json中的字符串。-请参阅我的答案,可能是重复的