+ Start a Discussion
Ayush_MangalAyush_Mangal 

JSON Parsing of Phone number

hello All,

 

Can anyone tell how to parse phone number from the following response:

 

DEBUG|HTTPResponse Body :{
"id":"SM32d3f91ca6d39323c3775d5c42207270",
"phone_id":"PNea453231d644aa3ee692511c1fb67a72",
"contact_id":"CT543e32d7b0e3a95a2d0ce8b9ae380f91",
"direction":"outgoing",
"status":"sent",
"message_type":"sms",
"time_created":1365770440,
"time_sent":1365770440,
"from_number":"5551212",
"to_number":"(989) 914-3756",
"content":"hiiii"
}

 

My Code:
while (parser.nextToken() != null) {
parser.nextToken();
parser.nextValue();

if( parser.getCurrentName() == 'to_number'){
newSMS.To__c = parser.getText();
System.debug('SMS TO: '+parser.getText());
}
if( parser.getCurrentName() == 'direction'){
newSMS.Direction__c = parser.getText();
System.debug('SMS Dir: '+parser.getText());
}
if( parser.getCurrentName() == 'message_type'){
newSMS.Type__c = parser.getText();
System.debug('SMS Dir: '+parser.getText());
}
if( parser.getCurrentName() == 'content'){
newSMS.Msg__c = parser.getText();
System.debug('SMS Msg: '+parser.getText());
}
if( parser.getCurrentName() == 'Status'){
newSMS.Status__c = parser.getText();
System.debug('SMS Statue: '+parser.getText());
}
}

 

I am able to parse other field, only not able to parse to_number. Can someone help?

sfdcfoxsfdcfox

Why don't you just deserialize into a map, like so:

 

map<string,string> info = (map<string,string>)json.deserialize(res.getbody(),map<string,string>.class);

You can get the phone via:

 

String phoneNumber = info.get('to_number');

Much faster than trying to parse everything yourself.

 

However, as to why 'to_number' isn't working, I don't have an answer for that. Your code looks like it should work.

Ayush_MangalAyush_Mangal

Thanks for the Ry.

Anyway i was able to get the parsing going right.!!