+ Start a Discussion
NagShaNagSha 

how to deserialize the JSON string and parse it's value to a specific field.,

Hi Team.,

 

I am NewBie trying to learn.,Any Help is appreciated.,

 

i have a custom field srmid__c in contact object,i am getting the json string as a response when i make a future callout.i need to update the srmid__c with this value.,

 

//Incoming JSON String: {"LocalDB_Key":5109740,"Salesforce_Key":"003Q000000ZsOynIAF","JRM_Object":"Contact","JRM_Field":"srmid__c"}


public class JSONParser {
public Integer LocalDB_Key {get;set;}
public String Salesforce_Key {get;set;}
public String JRM_Object {get;set;}
public String JRM_Field {get;set;}
Http h = new Http();
HttpRequest req = new HttpRequest();
String url = '******************************';
req.setMethod('POST');
req.setEndpoint(url);
req.setTimeout(120000);
HttpResponse res = h.send(req);
JSONParser parser = JSON.createParser(res.getBody());
while (parser.nextToken() != null) {
if (parser.getCurrentToken() == JSONToken.FIELD_NAME) {
String text = parser.getText();
parser.nextToken();
if (text == 'LocalDB_Key') {
this.LocalDB_Key = parser.getIntegerValue();
} else if (text == 'Salesforce_Key') {
this.Salesforce_Key = parser.getText();
} else if (text == 'JRM_Object') {
this.JRM_Object = parser.getText();
} else if (text == 'JRM_Field') {
this.JRM_Field = parser.getText();
}
}
}
}

SuperfellSuperfell

Is there a particular reason why you want to use the streaming parser and not data binding ?

 

I fed your sample json into http://json2apex.herokuapp.com/ and it generated this from it

 

public class JSON2Apex {

	public Integer LocalDB_Key;
	public String Salesforce_Key;
	public String JRM_Object;
	public String JRM_Field;

	
	public static JSON2Apex parse(String json) {
		return (JSON2Apex) System.JSON.deserialize(json, JSON2Apex.class);
	}
	
	static testMethod void testParse() {
		String json = '{\"LocalDB_Key\":5109740,\"Salesforce_Key\":\"003Q000000ZsOynIAF\",\"JRM_Object\":\"Contact\",\"JRM_Field\":\"srmid__c\"}';
		JSON2Apex obj = parse(json);
		System.assert(obj != null);
	}
}
NagShaNagSha

Hello Simon

 

First,Thanks for your Response.,

I tired that way already.,

Here is my code for that.,

 

Req: i need to send LocalDB_Key in JSON string to srmid in my Contact object.,

public class BCGResponseClass {

    @future(callout=true)
    public static void StudentData(list<Id> lstcust){
        String url ='*****************************;
        Http h = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint(url);
        req.setMethod('GET');
        req.setTimeout(120000);
        //req.setHeader('Content-Length', '512');
        HttpResponse res = h.send(req);
        string respbody = res.getbody();
        response(respbody);
    }
        public static void response(string body){
        //List<Contact> student = (List<Contact>)Json.deserialize(res.getbody(),List<Contact>.class);
        List<Contact> student = (List<Contact>)Json.deserialize(body,List<Contact>.class);
        }
        class details{
        public Integer LocalDB_Key {get;set;}
        public String Salesforce_Key {get;set;}
        public String JRM_Object {get;set;}
        public String JRM_Field {get;set;}
        public details(integer i,string ke,string oject,string Jid){
        this.LocalDB_Key=i;
        this.Salesforce_Key=ke;
        this.JRM_Object=oject;
        this.JRM_Field=Jid;
        }
    }                
    }

NagShaNagSha

HI all

 

public class Student{
  @future(callout=true)
    public static void sendingStudent(List<ID> studentids){ 
    
    map<Id, String> mapSfKeyCxKey = new map<Id, String>();
    
    list<Contact> lstCont = [Select c.Title, c.SystemModstamp, c.Salutation, c.ReportsToId, c.Ready_to_Migrate__c, c.Phone, c.OwnerId, c.OtherStreet, c.OtherState, c.OtherPostalCode, c.OtherPhone, c.OtherCountry, c.OtherCity, c.Name, c.MobilePhone, c.MasterRecordId, c.MailingStreet, c.MailingState, c.MailingPostalCode, c.MailingCountry, c.MailingCity, c.LeadSource, c.LastName, c.LastModifiedDate, c.LastModifiedById, c.LastCUUpdateDate, c.LastCURequestDate, c.LastActivityDate, c.JigsawContactId, c.Jigsaw, c.Jenzabar_ID__c, c.IsDeleted, c.Id, c.HomePhone, c.FirstName, c.Fax, c.EmailBouncedReason, c.EmailBouncedDate, c.Email, c.Description, c.Department, c.CreatedDate, c.CreatedById, c.ConnectionSentId, c.ConnectionReceivedId, c.Church__c, c.Branch_of_Military_Service__c, c.Birthdate, c.AssistantPhone, c.AssistantName, c.AccountId,c.Account.External_ID__c,c.Church__r.External_ID__c From Contact c where Id in :Studentids];
    String studentjson = JSON.serializepretty(lstcont);  
    System.debug('serialized Student JSON string'+studentjson);  
    HTTP h = new HTTP();
    HTTPRequest req = new HTTPRequest();
    String url ='********************';
    req.setEndpoint(url);
    req.setMethod('POST');
    req.setTimeout(60000);
    req.setBody(studentjson);
    HTTPResponse res = h.send(req);
    

 

if(res.getstatuscode() !=200){
       System.debug('Error from ' + req.getEndpoint() + ' : ' +res.getStatusCode() + ' ' + res.getStatus()); 
    }
        
    else {

 

String jsoninput = res.getbody();

 

jsoninput:

 

       {"JrmiStatus":{"Status":"IM","Error_Category":"Success","JRM_Key":"003Q000000ZsOynIAF","JRM_Object":"Contact","LocalDB_Key":"5109986","LocalDB_Table":"id_rec;profile_rec;multi_race_rec;aa_rec;site_rec","Error_Message":" "},

          "Responses":[{"Salesforce_Key":"003Q000000ZsOynIAF","JRM_Object":"Contact","JRM_Field":"Jenzabar_ID__c","LocalDB_Key":"5109986"}]}    
    

 

i need to insert the above json string into two objects JRMIStatus into Error object and Responses into Contact Object.

 

i am able todo parsing but not able to insert them into respective objects.      

 

//List<JrmiStatus> values = (List<JrmiStatus>)JSON.deserialize(res.getbody(),List<JrmiStatus>.class);

//List<Responses> values = (List<Responses>)JSON.deserialize(res.getbody(),List<Responses>.class);
SONParser parser = JSON.createParser(res.getBody());
           while (parser.nextToken() != JSONToken.END_OBJECT) {
                if((parser.getCurrentToken() == JSONToken.FIELD_NAME)&&(parser.getText() =='JrmiStatus')){
                    String text =parser.getText();
                    if(parser.nextToken() != JSONToken.VALUE_NULL){
                        if (text == 'Salesforce_Key') {
                             ID Salesforce_Key = parser.getIDValue();
                             if(!mapSfKeyCxKey.containsKey(Salesforce_Key)){
                               mapSfKeyCxKey.put(Salesforce_Key,null);
                             }
                        } else if (text == 'JRM_Object') {
                             String JRM_Object = parser.getText();
                        } else if (text == 'JRM_Field') {
                             String JRM_Field = parser.getText();
                        } else if (text == 'LocalDB_Key') {
                             Integer LocalDB_Key = parser.getIntegerValue();
                             //mapSfKeyCxKey.get(Salesforce_Key).add(LocalDB_Key);
                        }
                    }
                }else if ((parser.getCurrentToken() == JSONToken.FIELD_NAME)&&(parser.gettext() =='Responses')){
                      String text = parser.getText();  
                    if (parser.nextToken() != JSONToken.VALUE_NULL) {
                        if (text == 'Status') {
                            String Status = parser.getText();
                        } else if (text == 'Error_Category') {
                            String Error_Category = parser.getText();
                        } else if (text == 'JRM_Key') {
                            String JRM_Key = parser.getText();
                        } else if (text == 'JRM_Object') {
                            String JRM_Object = parser.getText();
                        } else if (text == 'LocalDB_Key') {
                            String LocalDB_Key = parser.getText();
                        } else if (text == 'LocalDB_Table') {
                            String LocalDB_Table = parser.getText();
                        } else if (text == 'Error_Message') {
                            String Error_Message = parser.getText();
                        }
                    }
                }
            }                               
        }
    }        
}