Apex (Salesforce) 从响应中读取 json
Apex (Salesforce) read json from response
您好,我有 return 我 json 的服务,格式为:
"{\"phoneIsValid\":true,\"IsMobile\":true,\"message\":\"success\"}"
我收到状态为 200 的响应(正常)
但是我如何从客户端 Apex 中的 json (phoneIsValid,IsMobile,message) 获取我的值。
这是我在 apex 中的代码(服务工作 100%)
JSONGenerator gen = JSON.createGenerator(true);
gen.writeStartObject();
gen.writeStringField('phone', '123466789');
gen.writeEndObject();
String jsonString= gen.getAsString();
HttpRequest req = new HttpRequest();
req.setMethod('POST');
req.setHeader('Content-Type', 'application/json');
req.setHeader('Accept', 'application/json');
req.setHeader('Content-Length',String.Valueof(jsonString.length()));
req.setHeader('Connection','keep-alive');
req.setHeader('Charset','utf-8');
req.setEndpoint('https://services.ValidatePhone');
req.setBody(jsonString);
Http http = new Http();
HTTPResponse res = new HTTPResponse();
res = http.send(req);
JSONParser parser = JSON.createParser(res.getBody());
//res.getBody() return -->
"{\"phoneIsValid\":true,\"IsMobile\":true,\"message\":\"success\"}"
if(res.getStatusCode() == 200)//i get 200,but cant find my value from Json
{
//here i need to get my values from Json
// SOME THINK LIKE THIS
// if(parser.phoneIsValid==true){ MY CODE}
}
我在这里错过了什么?
非常感谢。
您需要添加为您构建的 class Json pbject
public class JsonPhone {
public boolean phoneIsValid=false;
public boolean IsMobile=false;
public String message='empty';
}
稍微修改一下字符串,因为它与我的响应字符串不兼容
"{\"phoneIsValid\":true,\"IsMobile\":true,\"message\":\"success\"}"
String sResponseText = res.getBody();
sResponseText=sResponseText.replaceFirst('^\"+', '');
sResponseText=sResponseText.replaceFirst('\"+$', '');
sResponseText=sResponseText.replace('\', '');
JsonPhone jPhone = (JsonPhone)JSON.deserialize(sResponseText, JsonPhone.class);
jPhone 准备就绪。
您好,我有 return 我 json 的服务,格式为:
"{\"phoneIsValid\":true,\"IsMobile\":true,\"message\":\"success\"}"
我收到状态为 200 的响应(正常)
但是我如何从客户端 Apex 中的 json (phoneIsValid,IsMobile,message) 获取我的值。
这是我在 apex 中的代码(服务工作 100%)
JSONGenerator gen = JSON.createGenerator(true);
gen.writeStartObject();
gen.writeStringField('phone', '123466789');
gen.writeEndObject();
String jsonString= gen.getAsString();
HttpRequest req = new HttpRequest();
req.setMethod('POST');
req.setHeader('Content-Type', 'application/json');
req.setHeader('Accept', 'application/json');
req.setHeader('Content-Length',String.Valueof(jsonString.length()));
req.setHeader('Connection','keep-alive');
req.setHeader('Charset','utf-8');
req.setEndpoint('https://services.ValidatePhone');
req.setBody(jsonString);
Http http = new Http();
HTTPResponse res = new HTTPResponse();
res = http.send(req);
JSONParser parser = JSON.createParser(res.getBody());
//res.getBody() return -->
"{\"phoneIsValid\":true,\"IsMobile\":true,\"message\":\"success\"}"
if(res.getStatusCode() == 200)//i get 200,but cant find my value from Json
{
//here i need to get my values from Json
// SOME THINK LIKE THIS
// if(parser.phoneIsValid==true){ MY CODE}
}
我在这里错过了什么?
非常感谢。
您需要添加为您构建的 class Json pbject
public class JsonPhone {
public boolean phoneIsValid=false;
public boolean IsMobile=false;
public String message='empty';
}
稍微修改一下字符串,因为它与我的响应字符串不兼容
"{\"phoneIsValid\":true,\"IsMobile\":true,\"message\":\"success\"}"
String sResponseText = res.getBody();
sResponseText=sResponseText.replaceFirst('^\"+', '');
sResponseText=sResponseText.replaceFirst('\"+$', '');
sResponseText=sResponseText.replace('\', '');
JsonPhone jPhone = (JsonPhone)JSON.deserialize(sResponseText, JsonPhone.class);
jPhone 准备就绪。