4

I'm getting frustrated with an "Unknown field" error when I invoke deserializeStrict. Simple application, all strings...

My apex class contains this public class:

 public class CustomerInfo{ 
        public String CustomerNo {get; set;}
        public String CustomerName {get; set;}
        public String Address1 {get; set;}
        public String Address2 {get; set;}
        public String City {get; set;}
        public String State {get; set;}
        public String Zip {get; set;}
        public String County {get; set;}
        public String Country {get; set;}
        public String CreditBlock {get; set;}
        public String CreditLimit {get; set;}
    }

Field names are all correct, I have hard-coded the strings that are returned from the webservice... HTTPResponse res = h.send(req); CustomerInfo newCust = (CustomerInfo)System.JSON.deserializeStrict(res.getBody(), CustomerInfo.class);

The data comes back correctly formatted:

{"Address1":"address1 east 1st street","Address2":"suite 305","City":"Walnut","Country":"United States","County":"Douglas","CreditBlock":"No","CreditLimit":"500000","CustomerName":"My Test Customer","CustomerNo":"123456","State":"IA","Zip":"51323"}

Error in devloper console log:

"common.apex.runtime.impl.ExecutionException: Unknown field: JSONTest.CustomerInfo.GetCustomerInfoResult"|0x569159be

ANY help is greatly appreciated! I don't know what else to do, if I use deserialze rather than deserializeStrict all values in the new object are null...

  • sounds like the response has more data in it that you're showing. add a System.debug(res.getBody()) and see what that says – superfell Jan 7 '14 at 18:34
  • The data I have displayed is from the System.debug: 12:01:51:130 USER_DEBUG [77]|DEBUG|Fulfillment service returned {"GetCustomerInfoResult":{"Address1":"address1 east 1st street","Address2":"suite 305","City":"Walnut","Country":"United States","County":"Douglas","CreditBlock":"True","CreditLimit":"500000","CustomerName":"My Test Customer","CustomerNo":"123456","State":"IA","Zip":"51323"}} – scooterDoodle Jan 7 '14 at 18:45
  • 1
    You're trying to deserialize the GetCustomerInfoResult rather than the CustomerInfo bit, it will work fine if you get the child JSON contained in the GetCustomerInfoResult – Phil Hawthorn Jan 7 '14 at 18:49
  • Thanks for the response, my first experience with json obviously...I have a method: public static void getCustomerInfo(string custid) that makes a call to a WCF web service that returns "GetCustomerInfoResult":....so how do I navigate to the child string? – scooterDoodle Jan 7 '14 at 19:08
  • Also, json2apex.herokuapp.com is a great resource for quickly converting JSON to a simple Apex class. – Mike Chale Jan 7 '14 at 19:09
3

As your customerInfo data is nested inside another object in the JSON, you'll need to match that in your apex class structure, so you'll need to add an additional class,

public class CustomerInfoResult {
    public CustomerInfo GetCustomerInfoResult;
}

then you can deserialize it and access the nested result e.g.

CustomerInfo i = ((CustomerInfoResult)JSON.deserializeString(res.getBody(), CustomerInfoResult.class)).GetCustomerInfoResult;

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.