You need to sign in to do that
Don't have an account?
Varsha D 9
Updating record for deserialized json
public class Api4 {
public static void api(){
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setMethod('GET');
req.setEndpoint('https://demo1691926.mockable.io/Api12');
req.setHeader('Content-Type', 'application/json');
HttpResponse response = h.send(req);
system.debug('response : '+response.getBody());
String json1= response.getBody();
DesearlizingJson desearilize1=(DesearlizingJson) Json.deserialize(json1,DesearlizingJson.Class);
System.debug('Phone is ' +desearilize1.phone);
System.debug('name is ' +desearilize1.Name);
System.debug('mobile is ' +desearilize1.MobilePhone);
//trying to update deserialized json
list<contact> lcon= new list<contact>();
List<contact> lcon1 = [select id,Name,MobilePhone,Phone from contact where LastName=:desearilize1.Name];
for(contact c1:lcon1){
c1.Phone=desearilize1.phone;
lcon.add(c1);}
update c1;
}
}
Wrapper class
public class DesearlizingJson {
public Decimal Phone;
public String Name;
public Decimal MobilePhone;
}
Thanks in Advance
public static void api(){
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setMethod('GET');
req.setEndpoint('https://demo1691926.mockable.io/Api12');
req.setHeader('Content-Type', 'application/json');
HttpResponse response = h.send(req);
system.debug('response : '+response.getBody());
String json1= response.getBody();
DesearlizingJson desearilize1=(DesearlizingJson) Json.deserialize(json1,DesearlizingJson.Class);
System.debug('Phone is ' +desearilize1.phone);
System.debug('name is ' +desearilize1.Name);
System.debug('mobile is ' +desearilize1.MobilePhone);
//trying to update deserialized json
list<contact> lcon= new list<contact>();
List<contact> lcon1 = [select id,Name,MobilePhone,Phone from contact where LastName=:desearilize1.Name];
for(contact c1:lcon1){
c1.Phone=desearilize1.phone;
lcon.add(c1);}
update c1;
}
}
Wrapper class
public class DesearlizingJson {
public Decimal Phone;
public String Name;
public Decimal MobilePhone;
}
Thanks in Advance
I've reviewed your code and it seems that your code is correct, I want to know what is your problem?
Is there any error which you are getting or your contact field is not updating please let me know what is
your issue so that I can help you?
If you find your Solution then mark this as the best answer.
Thank you!
Regards
Suraj Tripathi
public static void api(){
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setMethod('GET');
req.setEndpoint('https://demo1691926.mockable.io/Api12');
req.setHeader('Content-Type', 'application/json');
HttpResponse response = h.send(req);
system.debug('response : '+response.getBody());
String json1= response.getBody();
DesearlizingJson desearilize1=(DesearlizingJson) Json.deserialize(json1,DesearlizingJson.Class);
System.debug('Phone is ' +desearilize1.phone);
System.debug('name is ' +desearilize1.Name);
System.debug('mobile is ' +desearilize1.MobilePhone);
List<DesearlizingJson> dj =desearilize1.Name; //Error :Illegal assignment from String to List<DesearlizingJson>
List<contact> con1 = new List<contact>();
for(DesearlizingJson c1 : dj){
Contact c=new Contact();
c.Name = c1.Name; //Error: Illegal assignment from Decimal to String
con1.add(c);
}
insert con1;
}
}
//m tryin to insert a record for the deserialized json response
getting the above errors while conversion ....can u help me out with this
Thanksalot in Advance
Please try the below updated codes:
Thanks,
Maharajan.C
If the JSON data does not contain a value for a field, the serializer does not change that field’s value. This method allows you to keep allocations to a minimum by reusing objects that you created previously. It also allows you to “patch” objects by deliberately overwriting them with JSON that only contains a small subset of fields.
Memory usage for garbage collection (GC) is at a minimum:
ToJson allocates GC memory only for the returned string.
FromJson allocates GC memory only for the returned object, as well as any subobjects needed (for example, if DGCustomerFirst (https://www.dgcustomerfirst.ltd/) you deserialize an object that contains an array, then Unity allocates GC memory for the array).
FromJsonOverwrite allocates GC memory only as necessary for written fields (for example, strings and arrays). This means that Unity does not allocate any GC memory at all if all the fields being overwritten by the JSON are value-typed.
You can use the JsonUtility API from a background thread. However, as with any multithreaded code, be careful not to access or alter an object on one thread while another thread is serializing or deserializing it.