0

In JsnoToApex class I try to GET JSON file from https://openweathermap.org/current. When I try to parse JSON.CreateParses I get these error: Illegal assignment from System.JSONParser to JsonParser

I try to make API that show weather in London.

public with sharing class JsonToApex {

@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);

}

}

I expect to get Json file and in future map it using https://json2apex.herokuapp.com/

2 Answers2

0

The problem was in first JSONParser. I had to add System.JSONParser to make it work.

System.JSONParser parser = JSON.createParser(resp);
0

You may have two approaches:

  1. If you have already map the JSON into an Apex class, you may just use
JSON2Apex aClass = (JSON2Apex) System.JSON.deserialize(resp, JSON2Apex.class);

Where JSON2Apex is the mapping Apex class.

  1. Use the JSON.deserializeUntyped(String jsonString) to deserialize the JSON to any Object in Apex class, like:
Map<String, Object> objMap = (Map<String, Object>) JSON.deserializeUntyped(resp);
Lushang
  • 51
  • 6