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
Raghu PedabbaRaghu Pedabba 

JSON deserialization

Hi Gurus,

I am trying to do a Call Out using REST.

My JSON
[{"eid":4,"name":"Mark"},{"eid":3,"name":"Steve"},{"eid":1,"name":"Peter"},{"eid":7,"name":"Matt"},{"eid":6,"name":"Barrow"},{"eid":5,"name":"Sugar"},{"eid":2,"name":"Thomas"}]

//
// Generated by JSON2Apex http://json2apex.herokuapp.com/
//
     public class JSON2Apex {
 
             public Integer id;
             public String name;
               public static JSON2Apex parse(String json) {
                   return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
            }
}

In execute anoymous window, I am trying the below code
HttpRequest req = new HttpRequest();
req.setEndpoint('http://********/emp/');
req.setMethod('GET');

String Username =  '*****';
String password = '*******';
 
Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'Basic ' +
EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);
   
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug(res.getBody());
 String responseStrng = res.getBody();
List<JSON2Apex> Emp = (List<JSON2Apex>)JSON.deserialize(responseStrng, List<JSON2Apex>.class);
system.debug(emp);

But, I am not getting any value in list emp ??

Not sure where I am going wrong. Please help.


 
Pankaj_GanwaniPankaj_Ganwani
Hi,

Can you just give it a try with below code snippet:

public class JSON2Apex {
 
             public Integer eid;
             public String name;
}
Daniel BallingerDaniel Ballinger
In your sample code you have
{"eid":4,"name":"Mark"}

Note the eid rather than id as appears in your current JSON2Apex class.

As Pankaj_Ganwani suggests, you need to modifiy your JSON2Apex class so the property name matches the JSON.
 
Raghu PedabbaRaghu Pedabba
Thanks Guys Solved the Problem