2) Apex Class
=====================================================================================
public class JsonSecondController {
String name;
public PageReference test() {
return null;
}
public PageReference submit() {
return null;
}
public String getName() { return name;}
public void setName(String nname) { name=nname;}
public ListgetOptions()
{
ListdefaultResult= new List ();
defaultResult.add(new SelectOption('','---'));
listlvalues;
try
{
String values=getJsonString('342096656648874');
lvalues=JSONObject.jsonarray ( new JSONObject.JSONTokener( values ) );
}
catch(Exception e)
{
return defaultResult;
}
return getSelectOptions(lvalues);
}
private String getJsonString(String id) {
String json;
HttpRequest req = new HttpRequest();
Http http = new Http();
req.setMethod('GET');
// generate the url for the request
String url = 'http://datarepository/page.aspx/GetIndustries/'+id;
req.setEndpoint(url);// add the endpoint to the request
req.setCompressed(true);
HTTPResponse resp = http.send(req);
json = resp.getBody().replace('\n', '');
try {
//System.debug('Respond code: ' + resp.getStatus());
return json;
} catch (JSONObject.JSONException e) {
return 'Error parsing JSON response: '+e;
}
}
private ListgetSelectOptions(list l)
{
Listelements=new List ();
for (Integer i = 0; i < l.size(); i++){
JSONObject v = l.get(i).obj;
String value=v.getValue('Value').str;
String label=v.getValue('Text').str;
elements.add(new SelectOption(value,label));
}
return elements;
}
}
=====================================================================================
2) Apex Page
=====================================================================================
<apex:page controller="JsonSecondController">
<apex:form >
<apex:selectList id="idindustry" value="{!name}" size="1">
<apex:selectOptions value="{!Options}"/>
<apex:commandButton value="Test" action="{!test}" rerender="out" status="status"/>
<apex:outputPanel id="out">
<apex:actionstatus id="status" startText="testing...">
=====================================================================================
References:
[1] JSONObject http://www.embracingthecloud.com/CommentView,guid,6ca59b34-f896-48a2-b1bf-33ffbe96b4ec.aspx
[2] http://wiki.developerforce.com/index.php/Building_Android_Applications_with_the_Force.com_REST_API
[3] JSON Validator page http://www.jsonlint.com/
No comments:
Post a Comment