i have json string like this downbelow


Now i need to parse them as like

in | loc
mmm| 1234
mmm| 1234

So far i did

public with sharing class Search 
    public String strTag {get;set;}
    public String strlocation {get;set;}
    public String result {get;set;}

    public PageReference find() {         

    HttpRequest req = new HttpRequest();
    HttpResponse res = new HttpResponse();
    Http http = new Http();


    //these parts of the POST you may want to customize
    req.setHeader('Content-Type', 'application/x-www-form-urlencoded');  

    try {
        res = http.send(req);       
    } catch(System.CalloutException e) {
        system.debug('Callout error: '+ e);
        result = ''+e;

    Result results = (Result) JSON.deserialize(res.getBody(),ResultSet.class);

    result = res.getBody();
        return null;

    public class ResultSet{       
        public List<Result> resultSet;

    public class Result
        public String ins;
        public String loc;

But its returns

System.TypeException: Invalid conversion from runtime type Search.ResultSet to Search.Result

How can i solved this problem

Thanks in advance

  • issue with JSON.deserialize(res.getBody(),ResultSet.class).. debug res.getBody() and create a class for deserialize – Ratan Paul Jan 5 '16 at 10:26

The problem is on this line:

Result results = (Result) JSON.deserialize(res.getBody(),ResultSet.class);

You initialize a variable of type Result and try to cast the result of JSON.deserialize() to a Result. However, the second parameter of the JSON.deserialize() function makes the function return an instance of ResultSet.

I think you want to change the type of the variable to ResultSet.

There are some more problems, however. The ResultSet class should not have a List<Result> field, but a Map<String, Result>, because the JSON also has a map. Also, your Result class has a field called ins, but in the JSON the name is in.

| improve this answer | |
  • it's worked.But how can i bind them with pageblocktable – moin khan Jan 5 '16 at 11:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.