【问题标题】:Parse Nested JSON and replace data in C#解析嵌套 JSON 并替换 C# 中的数据
【发布时间】:2017-01-20 18:22:30
【问题描述】:

我有一个复杂的 JSON 响应,我需要解析和替换其中的数据。

这只是 1 天的一个片段,但需要在响应中完成所有天。

我怎样才能解析这个并改变值。 我需要修改并发送回主机。

  {
  "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 格式保存编辑过的文件(这根本不起作用)`

已解决。 感谢您的反馈

【问题讨论】:

  • 由于某种原因你不能使用Newtonsoft.Json吗?
  • 在c#中创建一个模型来匹配json中的数据。使用 Newtonsoft.Json 之类的东西(如已经建议的那样)然后将传入的 Json 解析为对象。更改您需要的任何内容,然后再次使用它将对象转换回 json。
  • 使用 newtonsoft 的库即可。创建一个与 JSON 模式匹配的模型然后您要做的是针对该模型进行序列化或反序列化。您的模型将填充数据,“反序列化”或获取填充模型并将其序列化回 json 中的字符串。 - 参考我的回答stackoverflow.com/questions/21885243/…

标签: c# json


【解决方案1】:

在您的应用程序中创建以下类

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; }
}

然后使用 Newtonsoft.Json 像这样反序列化你的 json

public static T DeserializeObject<T>(string jsonObj)
{
    return JsonConvert.DeserializeObject<T>(jsonObj,
        new JsonSerializerSettings() { ReferenceLoopHandling = ReferenceLoopHandling.Ignore, Formatting = Formatting.Indented });
}

然后这样称呼它

RootObject obj = DeserializeObject<RootObject>(your_json);

然后访问您的日程安排。

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-01-19
    • 1970-01-01
    • 1970-01-01
    • 2021-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多