首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从System.JSONParser到JsonParser的赋值非法

从System.JSONParser到JsonParser的赋值非法
EN

Stack Overflow用户
提问于 2019-04-16 04:00:36
回答 2查看 1.1K关注 0票数 0

在JsnoToApex类中,我尝试从https://openweathermap.org/current获取JSON文件。当我尝试解析JSON.CreateParses时,我得到这些错误:从System.JSONParser到JsonParser的非法赋值

我试着制作能显示伦敦天气的API。

具有共享类JsonToApex的公共{

代码语言:javascript
复制
@future(callout=true)
public static void parseJSONResponse() {

    String resp;

    Http httpProtocol = new Http();
    // Create HTTP request to send.
    HttpRequest request = new HttpRequest();
    // Set the endpoint URL.
    String endpoint = 'https://samples.openweathermap.org/data/2.5/weather?q=London,uk&appid=b6907d289e10d714a6e88b30761fae22';
    request.setEndPoint(endpoint);
    // Set the HTTP verb to GET.
    request.setMethod('GET');
    // Send the HTTP request and get the response.
    // The response is in JSON format.
    HttpResponse response = httpProtocol.send(request);

    resp = response.getBody();
    JSONParser parser = JSON.createParser(resp);


    JsonMapper response = (JsonMapper) System.JSON.deserialize(res.getBody(), JsonMapper.class);

}

}

我希望得到Json文件,并在将来使用https://json2apex.herokuapp.com/映射它。

EN

回答 2

Stack Overflow用户

发布于 2019-04-16 05:08:34

问题出在first JSONParser中。我必须添加System.JSONParser才能使其工作。

代码语言:javascript
复制
System.JSONParser parser = JSON.createParser(resp);
票数 0
EN

Stack Overflow用户

发布于 2019-04-16 11:36:40

您可能有两种方法:

  1. 如果您已经将JSON映射到Apex类中,则可以只使用

代码语言:javascript
复制
JSON2Apex aClass = (JSON2Apex) System.JSON.deserialize(resp, JSON2Apex.class);

其中JSON2Apex是映射Apex类。

  1. 使用JSON.deserializeUntyped(String jsonString)将JSON反序列化为Apex类中的任何Object,如:

代码语言:javascript
复制
Map<String, Object> objMap = (Map<String, Object>) JSON.deserializeUntyped(resp);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55696441

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档