+ Start a Discussion
DeepikareddyDeepikareddy 

Generating the simple json using the json generator

Hi...! need to generate the salesforce simple json....i had embeded a page in the other page.. and trying to generate the json .. 

Page:1:
<apex:page controller="controllertest1">
  <apex:form >
   Enter Name:<apex:inputText value="{!statedet.stateName}"/>
  </apex:form>
</apex:page>
Page:2  : called page1 in page2
<apex:page controller="controllertest1">
 <apex:include pageName="testpage1"/>
  <br/>
  <apex:form >
   <apex:commandButton value="JSON GENERATOR"  action="{!genereatejson}" reRender="tm" />
  <br/>
   <apex:outputLabel id="tm"> {!jsonstrng}</apex:outputLabel>
  </apex:form>
</apex:page>

Commoncontroller:
public class controllertest1{
   public statedetails statedet{get;set;}
   public string jsonstrng{get;set;}
   public  controllertest1(){
     string  teststring= ' { "sucess":1,"data": {"stateName": "Andrapradesh",  "value": "apx" ,"rating":5 } } ';
     Map<String, Object> maptest =   (Map<String, Object>) JSON.deserializeUntyped(teststring);
     Object ob = (Object)maptest.get('data');
     String srlze  = System.JSON.serialize(ob);
     statedet= (statedetails)System.JSON.deserialize(srlze,statedetails .class);
    }
 public void genereatejson(){
          JSONGenerator gen = JSON.createGenerator(true);
          gen.writeStartObject();
          gen.writeStringField('stateName',statedet.stateName);
          gen.writeEndObject();
          jsonstrng = gen.getasString();
   }
      //wrapperclass
       public  class statedetails{
       public string stateName{get;set;}
       public string value{get;set;}
       public integer rating{get;set;}
       }
}

Note: After editing the input and clicking on the Button, iam not getting the changed  dynamic format . 
thanks 
deepika
 
Annu ChoudharyAnnu Choudhary
Hi Deepika,

Can you please try this one it is working for you. If you like this solution please select it as the best answer.

Page:1:
<apex:page controller="controllertest1">
    <apex:form >
   Enter Name:<apex:inputText value="{!statedet.stateName}"/>
        <br/>
        <apex:commandButton value="JSON GENERATOR"  action="{!genereatejson}"  />
  </apex:form>
</apex:page>

Page 2 :
<apex:page controller="controllertest1">
 <apex:include pageName="testpage1"/>
  <br/>
  <apex:form >
     <apex:actionSupport event="onClick"
                        action="{!genereatejson}"
                        rerender="tm"/>
   <apex:outputLabel id="tm"> {!jsonstrng}</apex:outputLabel>
  </apex:form>
</apex:page>
Common controller:
public class controllertest1{
   public statedetails statedet{get;set;}
   public string jsonstrng{get;set;}
   public  controllertest1(){
     string  teststring= ' { "sucess":1,"data": {"stateName": "Andrapradesh",  "value": "apx" ,"rating":5 } } ';
     Map<String, Object> maptest =   (Map<String, Object>) JSON.deserializeUntyped(teststring);
     Object ob = (Object)maptest.get('data');
     String srlze  = System.JSON.serialize(ob);
     statedet= (statedetails)System.JSON.deserialize(srlze,statedetails .class);
       System.debug(statedet);
    }
 public void genereatejson(){
          JSONGenerator gen = JSON.createGenerator(true);
          gen.writeStartObject();
          gen.writeStringField('stateName',statedet.stateName);
          gen.writeEndObject();
          jsonstrng = gen.getasString();
   }
      //wrapperclass
       public  class statedetails{
       public string stateName{get;set;}
       public string value{get;set;}
       public integer rating{get;set;}
       }
}
DeepikareddyDeepikareddy
Hi..Annu chowdary, the solution you had provided is good, you had used the commandbutton in the includedpage,  but i have multiple pages to embed, i jst need the single Command button to bind values for all the pages..Hope you understand...!

Thnaks.
Deepika..!