locked
Reading a Json string using Jobject RRS feed

  • Question

  • User1133338985 posted

    Hello All,

    I am using Newtonsoft_Json in my rest service and I am wanting to read this Json string I am getting back, but not sure how.

    I am reading in my Request by using: string result = await Request.Content.ReadAsStringAsync();

    I don't know if this is correct or not either.

    I  am wanting to get the LastmodifiedDate, Plaintif_Full_Name ect..

    The TotalSize and Done would also be nice to have.

    {"totalSize":3,"done":true,"records":[{"attributes":{"type":"Case","url":"/services/data/v40.0/sobjects/Case/500g000000G3NmpAAF"},"LastModifiedDate":"2017-08-25T14:57:51.000+0000","Plaintiff_Full_Name__c":"Devin Bolden","Plaintiff_Last_Name__c":"Bolden"},{"attributes":{"type":"Case","url":"/services/data/v40.0/sobjects/Case/500g000000G3NmqAAF"},"LastModifiedDate":"2017-07-17T15:26:51.000+0000","Plaintiff_Full_Name__c":"Jesus Flores","Plaintiff_Last_Name__c":"Flores"},{"attributes":{"type":"Case","url":"/services/data/v40.0/sobjects/Case/500g000000G3NmrAAF"},"LastModifiedDate":"2017-09-01T14:57:30.000+0000","Plaintiff_Full_Name__c":"Roderick Mosely","Plaintiff_Last_Name__c":"Mosely"}]}

    Thanks,

    Kss

    Thursday, October 5, 2017 3:40 PM

Answers

  • User2103319870 posted

    I  am wanting to get the LastmodifiedDate, Plaintif_Full_Name ect..

    The TotalSize and Done would also be nice to have.

    You can try with the below code

    //your Json string
                    string result = "{'totalSize':3,'done':true,'records':[{'attributes':{'type':'Case','url':'/services/data/v40.0/sobjects/Case/500g000000G3NmpAAF'},'LastModifiedDate':'2017-08-25T14:57:51.000+0000','Plaintiff_Full_Name__c':'Devin Bolden','Plaintiff_Last_Name__c':'Bolden'},{'attributes':{'type':'Case','url':'/services/data/v40.0/sobjects/Case/500g000000G3NmqAAF'},'LastModifiedDate':'2017-07-17T15:26:51.000+0000','Plaintiff_Full_Name__c':'Jesus Flores','Plaintiff_Last_Name__c':'Flores'},{'attributes':{'type':'Case','url':'/services/data/v40.0/sobjects/Case/500g000000G3NmrAAF'},'LastModifiedDate':'2017-09-01T14:57:30.000+0000','Plaintiff_Full_Name__c':'Roderick Mosely','Plaintiff_Last_Name__c':'Mosely'}]}";
    
                    //parsing Json 
                    Newtonsoft.Json.Linq.JToken token = Newtonsoft.Json.Linq.JObject.Parse(result);
                    //Get total size and done values
                    int totalSize = (int)token.SelectToken("totalSize");
                    bool done = (bool)token.SelectToken("done");
                    Newtonsoft.Json.Linq.JToken records = token.SelectToken("records");
                    //List to hold the values
                    List<JsonValues> list = new List<JsonValues>();
                    //Loop through each child items
                    foreach (Newtonsoft.Json.Linq.JToken childitem in records.Children())
                    {
                        //Initializing class object
                        JsonValues obj = new JsonValues();
                        //Fetch records for each item
                        obj.LastModifiedDate = (DateTime)childitem.SelectToken("LastModifiedDate");
                        obj.Plaintiff_Full_Name__c = (string)childitem.SelectToken("Plaintiff_Full_Name__c");
                        obj.Plaintiff_Last_Name__c = (string)childitem.SelectToken("Plaintiff_Last_Name__c");
                        //Add the result to your list
                        list.Add(obj);
                    }

    Class to hold the json collection values

    public class JsonValues
        {
            public string Plaintiff_Full_Name__c { get; set; }
            public string Plaintiff_Last_Name__c { get; set; }
            public DateTime LastModifiedDate { get; set; }
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 5, 2017 4:39 PM