0

I am stuck as I have a response in the format shown below. Am not able to deserialize the fields appended with __c.

"ReviewDetail": [{
        "Request_Id__c": "",
        "Material_Requested_No__c": " ",
        "Material_Desc__c": " ”,
        "Quantity__c":"",
        "Unit__c":"",
        "Location":[{
            "Location_No__c":"",
            "Qty_In_Location__c":"",
            "Location_Desc__c":"",
            "Location_Type__c":"""
        }]
    },
        "Request_Id__c": "",
        "Material_Requested_No__c": " ",
        "Material_Desc__c": " ”,
        "Quantity__c":"",
        "Unit__c":"",
            "Location":[{
            "Location_No__c":"",
            "Qty_In_Location__c":"",
            "Location_Desc__c":"",
            "Location_Type__c":"""
}]]
}

And when I Try to deserialize this response through fields am not able to do it as it throws the error that "Request_Id__c is not an invalid identifier.

public class TrunkStockInterfaceWrapper {
    public class Location {
        public Integer Location_No__c {get; set;}
        public Integer Qty_In_Location {get; set;}
        public String Location_Desc {get; set;}
        public String Location_Type {get; set;}


    }

    public class ReviewDetail {
        public Integer Request_Id {get; set;}
        public Integer Material_Requested_No {get; set;}
        public String Material_Desc {get; set;}
        public Integer Quantity {get; set;}
        public Integer Unit {get; set;}
        public List<Location> Location {get; set;}
    }

    public List<ReviewDetail> ReviewDetail {get; set;}
}

2 Answers 2

1

This is due to the fact that your wrapper class variable names are different than what it is in the JSON.

Request_Id__c of JSON doesn't match with Request_Id of wrapper class ReviewDetail, same goes for every other variable.

You can change your wrapper class's variable names to match the JSON variable names so that the de-serialization method can find the matching fields.

Needless to say, do the same for Location class as well.

2
  • Yes , You are right the json has Request_Id__c . But if I try to write this into my wrapper class then it throws the error that "Request_Id__c is not an invalid identifier May 12, 2017 at 8:03
  • Can you update your question with the wrapper class you are trying to save, with correct matching variable names? May 12, 2017 at 9:32
1

The fact that the fields of your JSON object end in __c seems to indicate that this JSON is actually a serialization of a Salesforce custom object, not a class. I would look inside your Salesforce org to see if you have a custom objec that matches those fields (possibly called ReviewDetail?).

Otherwise, your best bet would be to create a custom object that matches those fields, and try to deserialize to it. Also, bear in mind you're deserializing an array, not a single object.

You can find some code for a similar solution here: JSON String to custom object list

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.