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
S S MalikS S Malik 

Line: 13, Column: 22 Loop must iterate over a collection type: String(API integration starting issues)

Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://www.itutorlms.com/test.php');
request.setMethod('GET');
HttpResponse response = http.send(request);
// If the request is successful, parse the JSON response.
if (response.getStatusCode() == 200) {
    // Deserialize the JSON string into collections of primitive data types.
    Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
    // Cast the values in the 'Name' key as a list
    List<String> names = (List<String>) results.get('names');
    System.debug('Received the following names:');
    for(Object name :'names')
        {
        System.debug(name);
        }
}
Best Answer chosen by S S Malik
Amit Chaudhary 8Amit Chaudhary 8
Please update your code like below
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://www.itutorlms.com/test.php');
request.setMethod('GET');
HttpResponse response = http.send(request);
if (response.getStatusCode() == 200) 
{
    Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
	if(results != null && results.containsKey('names') )
	{
		List<String> names = (List<String>) results.get('names');
		for(String name :names)
		{
			System.debug(names);
		}
		
	}		
}
Let us know if this will help you
 

All Answers

sfdcMonkey.comsfdcMonkey.com
hi S S Malik 
try this
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://www.itutorlms.com/test.php');
request.setMethod('GET');
HttpResponse response = http.send(request);
// If the request is successful, parse the JSON response.
if (response.getStatusCode() == 200) {
    // Deserialize the JSON string into collections of primitive data types.
    Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
    // Cast the values in the 'Name' key as a list
    List<String> names = (List<String>) results.get('names');
    System.debug('Received the following names:');
    for(Object name :names)
        {
        System.debug(names);
        }
}
Thanks
i hop its helps you Mark it best answer if it helps you :)
 
S S MalikS S Malik
Hi Piyush Soni 
Thank for quick reply but its gives this error
i had done this practice already but could not solve this
System.NullPointerException: Attempt to de-reference a null object
Amit Chaudhary 8Amit Chaudhary 8
Please update your code like below
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://www.itutorlms.com/test.php');
request.setMethod('GET');
HttpResponse response = http.send(request);
if (response.getStatusCode() == 200) 
{
    Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
	if(results != null && results.containsKey('names') )
	{
		List<String> names = (List<String>) results.get('names');
		for(String name :names)
		{
			System.debug(names);
		}
		
	}		
}
Let us know if this will help you
 
This was selected as the best answer