function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
VentilsVentils 

JSON RELATED

Hi,

 

I am a newbie Iknow nothing.. I am completely dumb.. The guy who knows nothing.

 

I have a Json which I got as a reply when I made a callout This JSON contains the access_token how can I write the code so as to store the value of this access_token in a string and so that I could move on.. Any Help here?

 

 

Regards

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox
public class json_response {
     public string access_token, error;
     public boolean success; // modify these fields as you need
}

public void do_json_login(...) {
    ...
    HttpResponse res = binding.send(req);
    json_response login_res = (json_response)json.deserialize(res.getbody(), json_response.class);
    // login_res.access_token contains your token.
    ...
}

This is the easiest way to achieve your goal; no complex parsing, guessing data types, etc.

All Answers

VentilsVentils
public static void oauthLogin() {
		HttpRequest req = new HttpRequest();
		req.setMethod('POST');
		req.setEndpoint('https://login.salesforce.com'+'/services/oauth2/token');
		req.setBody('grant_type=password' +
		'&client_id=3MVG9Y6d_Btp4dsadaxp73r1LES0LmaZqiwu6K2jn2Q8oqqKK.y.0VnbauohXTr1g0eFu2xtomsfNDqxTqjApkyt3t' +
		'&client_secret=3604977494422207476' +
		'&username=' + EncodingUtil.urlEncode('anup@paul.com', 'UTF-8') +
		'&password=' + EncodingUtil.urlEncode('hello1234', 'UTF-8'));
		Http http = new Http();
		HTTPResponse res = http.send(req);
			
		
}

 

PriyasoftPriyasoft

Please refer below link. I believe it will sove your problem of parsing JSON response.

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_jsonparser.htm

sfdcfoxsfdcfox
public class json_response {
     public string access_token, error;
     public boolean success; // modify these fields as you need
}

public void do_json_login(...) {
    ...
    HttpResponse res = binding.send(req);
    json_response login_res = (json_response)json.deserialize(res.getbody(), json_response.class);
    // login_res.access_token contains your token.
    ...
}

This is the easiest way to achieve your goal; no complex parsing, guessing data types, etc.

This was selected as the best answer