0

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

4
  • Can you not use Newtonsoft.Json for some reason? Commented Jan 20, 2017 at 18:24
  • Create a model in c# to match the data in json. Use something like Newtonsoft.Json (as already suggested) to then parse the incoming Json into objects. Change whatever you need and then use it again to convert the objects back into json. Commented Jan 20, 2017 at 18:26
  • Just use newtonsoft's library. Create a model that matches the JSON schema Then what you do is serialize or deserialize against that model. Your model will get populated with data, "deserialize" or get a populated model and serialize it back to a string in json. - Refer to my Answer stackoverflow.com/questions/21885243/… Commented Jan 20, 2017 at 18:26
  • 1
    Possible duplicate of How can I parse JSON with C#? Commented Jan 20, 2017 at 18:27

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.