0

i have json string like this downbelow

{"0":{"in":"mmm","loc":"1234"},"1":{"in":"mmm","loc":"1234"}}

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

    req.setEndpoint('http://test.3spire.net/index.php?in='+strTag+'&loc='+strlocation);
    req.setMethod('GET');

    //these parts of the POST you may want to customize
    req.setCompressed(false);
    req.setBody('key1=value1&key2=value2');
    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();
    system.debug(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

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

1 Answer 1

2

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.

1
  • it's worked.But how can i bind them with pageblocktable
    – moin khan
    Jan 5, 2016 at 11:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .