+ Start a Discussion
SARI4SARI4 

Convert Json string to Wrapper class and store in custom object

Hi
I have a Json string, I want to store it into custom object.
json string is:
'{"FRAPI":{"STATUS":"0","MSG":" ","ACCOUNTLIST":{"ACCOUNT":[{"ACCOUNTID":"1083302481D3","ACCOUNTNAME":"kelly Roosevelt","DEFAULTDATE":"20121231"},{"ACCOUNTID":"119334261AD3","ACCOUNTNAME":"Stephen King","DEFAULTDATE":"20121231"}]}}}'
I have written below code but I am getting null value in Wrapper output
global class json2Apex{
public string xmlstring{get;set;}
public string jsstring{get;set;}
public cls_FRAPI  Wrapper{get;set;}
public string resstring{get;set;}
public json2Apex(){
datapull();
}
public void datapull(){

    xmlstring= p.GetAccountList('TGAINES','FRdemo13!','DEMO201212','1');
  jsstring='{"FRAPI":{"STATUS":"0","MSG":" ","ACCOUNTLIST":{"ACCOUNT":[{"ACCOUNTID":"1083302481D3","ACCOUNTNAME":"Teddy Roosevelt","DEFAULTDATE":"20121231"},{"ACCOUNTID":"119334261AD3","ACCOUNTNAME":"Stephen King","DEFAULTDATE":"20121231"}]}}}';
      
      
      
       resstring=jsstring.replace('\\n','').replace('\\','').replace('"{','{').replace('}"','}');
      Wrapper =(cls_FRAPI ) System.JSON.deserialize(resstring, cls_FRAPI .class);
      System.debug('>>>>>'+Wrapper  );
 }  

   public cls_FRAPI FRAPI{get;set;}
    class cls_FRAPI {
        public String STATUS{get;set;}  //0
        public String MSG{get;set;}  // 
        public cls_ACCOUNTLIST ACCOUNTLIST{get;set;}
    }
    class cls_ACCOUNTLIST {
        public list<ACCOUNT> cls_ACCOUNT {get;set;}
    }
    class cls_ACCOUNT {
        public String ACCOUNTID{get;set;}    //1083302481D3
        public String ACCOUNTNAME{get;set;}  //Teddy Roosevelt
        public String DEFAULTDATE{get;set;} //20121231
    }
}


Result is:
cls_FRAPI:[ACCOUNTLIST=null, MSG=null, STATUS=null]

Please suggest the soluton.

Thanks
Best Answer chosen by SARI4
Jean-Noel CasassusJean-Noel Casassus
String jsstring='{"FRAPI":{"STATUS":"0","MSG":" ","ACCOUNTLIST":{"ACCOUNT":[{"ACCOUNTID":"1083302481D3","ACCOUNTNAME":"Teddy Roosevelt","DEFAULTDATE":"20121231"},{"ACCOUNTID":"119334261AD3","ACCOUNTNAME":"Stephen King","DEFAULTDATE":"20121231"}]}}}';

system.debug((cls_FRAPI) System.JSON.deserialize(jsstring, cls_FRAPI.class));

	public class ACCOUNT {
		public String ACCOUNTID;
		public String ACCOUNTNAME;
		public String DEFAULTDATE;
	}

	public class cls_FRAPI {
		public FRAPI FRAPI;
	}

	public class ACCOUNTLIST {
		public List<ACCOUNT> ACCOUNT;
	}

	public class FRAPI {
		public String STATUS;
		public String MSG;
		public ACCOUNTLIST ACCOUNTLIST;
	}

That should help