3

I have 52 keys and pairs in my JSON data, and I want to parse it, but the APEX method only supports 35 arguments to the method. Then how can I parse large JSON data with the APEX class?

My JSON is:

{"products":[{

"title":"SPF 50 for Body & Face 3-Pack",
"id":1952,
"created_at":"2015-11-19T23:12:45Z",
"updated_at":"2016-04-08T20:37:12Z"
.......  up to 52 key pair ....

},
{

"title":"SPF 50 for Body & Face 3-Pack",
"id":1952,
"created_at":"2015-11-19T23:12:45Z",
"updated_at":"2016-04-08T20:37:12Z"
.......  up to 52 key pair .....

},
{

"title":"SPF 50 for Body & Face 3-Pack",
"id":1952,
"created_at":"2015-11-19T23:12:45Z",
"updated_at":"2016-04-08T20:37:12Z"
.......  up to 52 key pair ....

}]}

My parser is:

List<WooProductModel> productList = new List<WooProductModel>();
        while (parser.nextToken() != null) {
            if (parser.getCurrentToken() == JSONToken.START_ARRAY) {
                while(parser.nextToken() != null)  {
                    if(parser.getCurrentToken() == JSONToken.START_OBJECT) {
                        WooProductModel product = (WooProductModel)parser.readValueAs(WooProductModel.class);
                        productList.add(product);
                        System.debug('data: ' + productList);
                    }
                }
            }
        }

My apex class is:

public class WooOrdersParser {

    /* I need to pass 52 arguments as my JSON has 52 key pair values */
    public WooOrdersParser(String id, .... only upto 32 argument ...)  {


    }
}

1 Answer 1

3

You can pass complex objects directly:

public class WooOrdersParser {
    public WooOrdersParser(WooProductModel product) {
        // ... product contains all 52 key-value pairs ...
    }
}

This has the added benefit of not having to deal with a ridiculously long parameter list, as well as the reduced risk of accidentally ordering the parameters wrong.

1
  • thanks for your answer. i have applied your code and it work perfectly.
    – Rajinder
    Commented May 16, 2016 at 7:00

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .