1

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 Answer 1

2

You are calling JSON.deserialize(res.getBody(),ResultSet.class). The second parameter ResultSet is the Apex object type you want the result to be. But then you attempt to cast it to a type of Result instead.

Either do

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

or

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

In your case, based on the JSON it would seem you want the second option. However, your JSON doesn't quite match your ResultSet class either. Your JSON is a map, not a list. Also, there's a field mismatch between "in" and "ins". This JSON is what would match your ResultSet class:

{{"ins":"mmm","loc":"1234"},{"ins":"mmm","loc":"1234"}}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.