Skip to main content
added 224 characters in body
Source Link
Nagendra Singh
  • 9.1k
  • 6
  • 29
  • 60

The return type .toString() does not generate a JSON formatted String.

You will have to change that to :

@RestResource(urlMapping='/Johnny')
global class WebServiceFactory {
    @HttpGet
    global static String getRecord() {
        List<JSONFactory> JSONs = new List<JSONFactory>();
        for(integer i = 0; i<4; i++){
            JSONs.add(new JSONFactory(i, 'Jan' + i, 'Test' + i));

        }
        return JSON.serialize(JSONs);
    }
}

Only then you will receive a JSON formatted String.

20:24:05.199 (202088269)|USER_DEBUG|[17]|DEBUG|[{"name":"Jan0","lastName":"Test0","id":0},{"name":"Jan1","lastName":"Test1","id":1},{"name":"Jan2","lastName":"Test2","id":2},{"name":"Jan3","lastName":"Test3","id":3}]

After this your List<Object> objs = (List<Object>)JSON.deserializeUntyped(res.getBody()); would work as expected.

The return type .toString() does not generate a JSON formatted String.

You will have to change that to :

@RestResource(urlMapping='/Johnny')
global class WebServiceFactory {
    @HttpGet
    global static String getRecord() {
        List<JSONFactory> JSONs = new List<JSONFactory>();
        for(integer i = 0; i<4; i++){
            JSONs.add(new JSONFactory(i, 'Jan' + i, 'Test' + i));

        }
        return JSON.serialize(JSONs);
    }
}

Only then you will receive a JSON formatted String.

After this your List<Object> objs = (List<Object>)JSON.deserializeUntyped(res.getBody()); would work as expected.

The return type .toString() does not generate a JSON formatted String.

You will have to change that to :

@RestResource(urlMapping='/Johnny')
global class WebServiceFactory {
    @HttpGet
    global static String getRecord() {
        List<JSONFactory> JSONs = new List<JSONFactory>();
        for(integer i = 0; i<4; i++){
            JSONs.add(new JSONFactory(i, 'Jan' + i, 'Test' + i));

        }
        return JSON.serialize(JSONs);
    }
}

Only then you will receive a JSON formatted String.

20:24:05.199 (202088269)|USER_DEBUG|[17]|DEBUG|[{"name":"Jan0","lastName":"Test0","id":0},{"name":"Jan1","lastName":"Test1","id":1},{"name":"Jan2","lastName":"Test2","id":2},{"name":"Jan3","lastName":"Test3","id":3}]

After this your List<Object> objs = (List<Object>)JSON.deserializeUntyped(res.getBody()); would work as expected.

Source Link
Nagendra Singh
  • 9.1k
  • 6
  • 29
  • 60

The return type .toString() does not generate a JSON formatted String.

You will have to change that to :

@RestResource(urlMapping='/Johnny')
global class WebServiceFactory {
    @HttpGet
    global static String getRecord() {
        List<JSONFactory> JSONs = new List<JSONFactory>();
        for(integer i = 0; i<4; i++){
            JSONs.add(new JSONFactory(i, 'Jan' + i, 'Test' + i));

        }
        return JSON.serialize(JSONs);
    }
}

Only then you will receive a JSON formatted String.

After this your List<Object> objs = (List<Object>)JSON.deserializeUntyped(res.getBody()); would work as expected.