Search code examples
angularjsjsonhttp-getspring-restcontroller

How to parse a Json array with values "id": 1.1.1.1


I have a $http get method which should invoke my local server. I am getting proper response from my server through postman. Following is my Json values:

[{"id":27885,"bslRef":acms.2016.04.00,"size":0,"isDefault":true,"baselineDate":null,"parentRef":null,"activities":null,"default":true,"open":true,"daily":false}]

As "bslRef":acms.2016.04.00 which is the value I am interested in is not enclosed in a json tag [""].

That is why i am getting an parsing error. This is the error I am getting from jsonlint.

Error: Parse error on line 3:
...: 27885, "bslRef": acms .2016 .04 .00,
----------------------^
Expecting 'STRING', 'NUMBER', 'NULL', 'TRUE', 'FALSE', '{', '[', got 'undefined'

Following is my angularjs controller code:

$http.get('/baseline/getBaseline?', {params: {
                       currProject: $scope.selectedProject}})
                   .success(function(data) {

                       $scope.baselines=JSON.stringify(data);
                   })
                   .error(function(data) {
                       alert("failure");
                   });

I have tried with parse() method and stringify() method but of no use.

My server side application is a spring-mvc rest application, which is giving me proper response, But in the said format.

Please some one help me either parse this through angular methods, or get me a json type response from my spring controller. TIA.


Solution

  • The issue got solved. Actually in my pojo class for Baseline I had added following Json annotation.

    private long id;
    @JsonSerialize(as = BaselineRef.class)
    @JsonRawValue
    private BaselineRef bslRef;
    private int size;
    @JsonProperty
    private boolean isDefault = false;
    private boolean isOpen = true;
    private boolean isDaily = false;
    private String baselineDate;
    private BaselineRef parentRef;
    private Collection<String> activities;
    

    Hence while retrieving I was getting following types of response.

    [{"id":27885,"bslRef":acms.2016.04.00,"size":0,"isDefault":true,"baselineDate":null,"parentRef":null,"activities":null,"default":true,"open":true,"daily":false}].
    

    Once i removed the annotation Now I am getting

     `[{"id":27885,"bslRef":{"bslName":"2016.04.00","projectName":"acms","overrides":false},"size":0,"baselineDate":null,"parentRef":null,"activities":null,"default":true,"open":true,"daily":false}]` 
    

    which is my desired response. Here i ensured that BaselineRef has an toString method. Here I dont have to parse in my angular code. Thanks for everyone help.