简体   繁体   中英

Parse Nested JSON and replace data in C#

I have a complex JSON response that i need to parse and replace data in.

This is just a snip from 1 day but need to do all days in the response.

How can i parse this and just change values. I need to modify and send back to the host.

  {
  "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"
    },

so what i have now is this:`

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();

}

i can get the data from first curly braces but no further. I need to get from each array. i also want to modify a given entry and then save the edited file with proper json formatting(this is not working at all)`

SOLVED. Thank you for your feedback

Create the following classes in your application

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

And then use Newtonsoft.Json to deserialize your json like

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

And call it like

RootObject obj = DeserializeObject<RootObject>(your_json);

then access your schedules.

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM