0

I'm trying to parse the json string into meaningful data over a VF page.

Here's where I'm stuck...

public class callApi{

private final String serviceEndpoint= 'http://XXX';
public String Response { get; set;}
public String Headers { get; set; }
public Boolean cache;
public List<Data> data { get; set; }
public Provider provider;

public class Data {
    public String unique_id {get;set;}
    public String description {get;set;}
    public String title {get;set;}
}

public class Provider {
    public String pubDate;
    public String source;
    public String url;
}        

public void fetchmyData() {
    getAndParse('GET');
}

public void getAndParse(String GET) {

Http http = new Http();
HttpRequest request = new HttpRequest();
String authorizationHeader = 'BASIC';
request.setEndpoint(serviceEndpoint);

request.setMethod('GET');
request.setHeader('Accept','text/html');


request.setHeader('Authorization', authorizationHeader);
request.setTimeout(120000);        

HttpResponse json= http.send(request);

System.debug(json.getBody());
this.response=json.getBody();
callApi temp = parse(this.response);
System.debug('@@temp'+temp);
List<Data> dr = new List<Data>();
for(Data d : temp){
unique_id = d.unique_id;
description = d.description;
unique_id = d.unique_id;  
dr.add(d);

}

}
 public static callApi parse(String json) {

System.debug('@@json'+json);
    return (callApi) System.JSON.deserialize(json, callApi.class);
}
}

And here's the VF page

   <apex:page controller="callApi">
    <apex:form >
    <apex:pageBlock title="Get News">
    <apex:pageBlockButtons title="News">
    <apex:commandButton value="Get News" action="{!fetchmyData}" reRender="myBlock" />
        </apex:pageBlockButtons>

        <apex:pageBlockSection id="myBlock" columns="1">
            <apex:repeat value="{!dr}" var="line">
                   <apex:outputText value="{!line.title}" id="theValue"/>
            </apex:repeat>
        </apex:pageBlockSection>

    </apex:pageBlock> 
</apex:form>
</apex:page>

I get the a proper response string in temp. But I'm unable to parse and format the values. I do get a proper string if I put the repeat values as "!response", but this is not what I need. I require the data inside such as unique_id, description and title. I get the following error:

Error: Compile Error: Variable does not exist: unique_id at line 49

I'm new to REST API, a help would be much appreciated! Thanks in advance.

  • What does the response look like? Did you JSON to Apex it? – EricSSH Aug 12 '16 at 20:51
  • Yes, I used JSON2Apex. The response is fine. SHould I post that too? – MnZ Aug 12 '16 at 20:53
  • could you post it? – EricSSH Aug 12 '16 at 20:56
  • here you go...pastie.org/10935308 – MnZ Aug 12 '16 at 21:04
2

You already got your data in property List<Data> data. You just have to find a way to map it to page.

Apex:

public class callApi {

    public List<Data> data;// { get; set; }
    public String Response;
    public class Data {
        public String unique_id {get;set;}
        public String description {get;set;}
        public String title {get;set;}
    } 
    public void fetchmyData() {
        getAndParse('GET');
    }
    public void getAndParse(String GET) {//
        this.response = '{  "cache": true,  "data": [{"unique_id": "997991","description": "Scotland v United Arab Emirates 212/5 *","title": "Scotland v United Arab Emirates 212/5 *" }, {"unique_id": "1041859","description": "Amo Sharks v Speen Ghar Tigers","title": "Amo Sharks v Speen Ghar Tigers"    }], "provider": {"pubDate": "2016-08-12T20:58:21.776Z","source": "Various","url": "http://cricapi.com/" }}';
        callApi temp = parse(this.response);    
        this.data = temp.data; 
    }
    public List<Data> getData(){
        return this.data;
    }
    public static callApi parse(String json) {
        return (callApi) System.JSON.deserialize(json, callApi.class);
    }
}

VF:

<apex:page controller="callApi">
    <apex:form >
        <apex:pageBlock title="Get News">
            <apex:pageBlockButtons title="News">
                <apex:commandButton value="Get News" action="{!fetchmyData}" reRender="myBlock" />
            </apex:pageBlockButtons>

            <apex:pageBlockSection id="myBlock" columns="1">
                <apex:repeat value="{!data}" var="line">
                    <apex:outputText value="{!line.title}" id="theValue"/>
                </apex:repeat>
            </apex:pageBlockSection>

        </apex:pageBlock> 
    </apex:form>
</apex:page>
  • Hi @ilya-lepesh. It worked! Thanks! – MnZ Aug 12 '16 at 21:37
2

Using JSON to Apex I built this Inner Class to use, I like to instantiate Lists in my inner classes so they aren't null.. This may be a personal preference

public class SportsReponseData
{
    public SportsReponseData()
    {
        data = new List<Data>();
    }

    public boolean cache {get;set;}
    public List<Data> data {get;set;}
    public Provider provider {get;set;}
}

public class Data 
{
    public String unique_id {get;set;}
    public String description {get;set;}    
    public String title {get;set;}  
}

public class Provider 
{
    public String pubDate {get;set;}    
    public String source {get;set;} 
    public String url {get;set;}    
} 

I then built a test run and ran it in Anonymous like so..

SportsReponseData result = new SportsReponseData();

String testJSON = '{"cache": true,"data": [{"unique_id": "1041859","description": "Amo Sharks v Speen Ghar Tigers","title": "Amo Sharks v Speen Ghar Tigers"}],"provider": {"pubDate": "2016-08-12T20:58:21.776Z","source": "Various","url": "http://cricapi.com/"}}';

result = (SportsReponseData)JSON.deserialize(testJSON,  SportsReponseData.class);

System.debug('result ' + result); 

And it returned the response into this

[cache=true, data=(Data:[description=Amo Sharks v Speen Ghar Tigers, title=Amo Sharks v Speen Ghar Tigers, unique_id=1041859]), provider=Provider:[pubDate=2016-08-12T20:58:21.776Z, source=Various, url=http://cricapi.com/]]

I only tested it with one, but it should be able to handle a list of your nodes, try it out I didn't want to build the string out, I'm not sure why yours didn't work but I did build my object differently

  • Attempting your way... – MnZ Aug 12 '16 at 21:25
  • Hey Eric, I understand what you mean to say. But my main concern is to display it on a VF page. I'm anyway getting a json response. Thanks for helping! – MnZ Aug 12 '16 at 21:32

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.