0

I am completely new to coding, bear with me if I make any stupid presumptions about this code I learnt through a youtube video. I am querying Salesforce through REST API and my response has been converted to a string. PART 1: I want to print it in pretty print JSON format and PART 2: store the JSON data into an object.

I haven't tried zilch yet but I am looking at some previous answers on this forum.

        HttpClient apiCallClient = new HttpClient();
        String restCallURL = serviceUrl + "/services/data/v45.0/query?q=SELECT+Id,PAL_ID__c+FROM+Account";
        HttpRequestMessage apirequest = new HttpRequestMessage(HttpMethod.Get, restCallURL);
        apirequest.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
        apirequest.Headers.Add("authorization","Bearer " + authToken);
        HttpResponseMessage apiCallResponse = await apiCallClient.SendAsync(apirequest);
        String requestresponse = await apiCallResponse.Content.ReadAsStringAsync();
        Console.WriteLine(requestresponse);
        Console_textBox.AppendText(requestresponse);

Just need in Pretty Print JSON on my console and to store that data in an object.

4

1 Answer 1

3

You can make a simple class to hold the Salesforce response, like:

public class SalesforceListResponse<T>
{
    [JsonProperty("totalSize")]
    public string TotalSize { get; set; }

    [JsonProperty("done")]
    public bool Done { get; set; }

    [JsonProperty("nextRecordsUrl")]
    public string NextRecordsUrl { get; set; }

    [JsonProperty("records")]
    public T[] Records { get; set; }
}

And make a simple class to hold the account:

public class Account
{
    public string Id { get; set; }

    public string PAL_ID__c { get; set; }

    ⋮
}

Then just deserialize it:

var salesforceResponse = JsonConvert.DeserializeObject<SalesforceListResponse<Account>>(requestresponse );
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.