0

Below is my code where I am calling the url having json data.

error is 'variable doesn't exist:UserId

public class jsonParser{
       public Double UserId;
       public  Double Id;
       public  String Title;
       public  String Body;

    public static void parse() {
        Http h = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndPoint('https://jsonplaceholder.typicode.com/posts');
        request.setHeader('Content-type', 'application/json');
        request.setMethod('GET');
        HttpResponse response = h.send(request);

    List<NewUsers__c> newallUsers = new List<NewUsers__c>();
    List<NewUsers__c> allUsers = (List<NewUsers__c>)JSON.deserialize(response.getBody(),List<NewUsers__c>.class);
    for( NewUsers__c user: allUsers){
        NewUsers__c newUser = new NewUsers__c();
        UserId__c = user.UserId;
            Id__c= user.Id;
            Title__c = user.Title;
            Body__c = user.Body;
        newallUsers.add(newUser);    

    }
    Database.insert(newallUsers);
}
}
0

You need to create a wrapper class to parse this JSON

public class NewUser{

       public Double UserId;
       public  Double Id;
       public  String Title;
       public  String Body;

}

and then in your code use it

List<NewUser> allUsers = (List<NewUser>)JSON.deserialize(response.getBody(),List<NewUser>.class);
    for( NewUser user: allUsers) {
        NewUsers__c newUser = new NewUsers__c();
        newUser.UserId__c = user.UserId;
        newUser.Id__c= user.Id;
        newUser.Title__c = user.Title;
        newUser.Body__c = user.Body;
        newallUsers.add(newUser);    
    }
0

The deserialize to SObject will fail as it requires "attributes" to be in the first position, just like in the response String(JSON). You need to generate an inner class to replicate your JSON structure in apex code and populate values.

You can use https://json2apex.herokuapp.com/ to generate the Class.

//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//

public class NewUser {

    public Integer userId;
    public Integer id;
    public String title;
    public String body;


    public static List<NewUser> parse(String json) {
        return (List<NewUser>) System.JSON.deserialize(json, List<NewUser>.class);
    }
}

If you see the Test class (generated along with the class) it gives you the code how to deserialize the JSON String to the List of this newly created Class. List<NewUser> obj = NewUser.parse(json);. You can loop through this list and populate your custom Object.

//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//

@IsTest
public class NewUser_Test {

    static testMethod void testParse() {
        String json = '['+
        '  {'+
        '    \"userId\": 1,'+
        '    \"id\": 1,'+
        '    \"title\": \"sunt aut facere repellat provident occaecati excepturi optio reprehenderit\",'+
        '    \"body\": \"quia et suscipit\\nsuscipit recusandae consequuntur expedita et cum\\nreprehenderit molestiae ut ut quas totam\\nnostrum rerum est autem sunt rem eveniet architecto\"'+
        '  },'+
..................
..................
        '    \"userId\": 10,'+
        '    \"id\": 100,'+
        '    \"title\": \"at nam consequatur ea labore ea harum\",'+
        '    \"body\": \"cupiditate quo est a modi nesciunt soluta\\nipsa voluptas error itaque dicta in\\nautem qui minus magnam et distinctio eum\\naccusamus ratione error aut\"'+
        '  }'+
        ']';
        List<NewUser> obj = NewUser.parse(json);
        System.assert(obj != null);
    }
}

You have to modify your code as below (Please note this is not compiled code)

List<NewUsers__c> newallUsers = new List<NewUsers__c>();
List<NewUser> allUsers = NewUser.parse(response.getBody());
for( NewUser user: allUsers){
    NewUsers__c newUser = new NewUsers__c();
    newUser.UserId__c = user.userId;
    newUser.Id__c= user.id;
    newUser.Title__c = user.title;
    newUser.Body__c = user.body;
    newallUsers.add(newUser);    
}
Database.insert(newallUsers);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.