ChiranSJ
ChiranSJ

Reputation: 145

Removing Some Characters In A JSON Response

I have a JSON response as the follwoing, but my problem is there are some characters that is not related to the JSON response I want. So I have pass that JSON response to a JavaScript variable and look into the JSON string. That is at the bottom.

-----------JSON Response------------

{
   "readyState":4,
   "responseText":"<?xml version=\"1.0\"encoding=\"utf-8\"?>\r\n<string>{\"kind\":\"analytics#gaData\",\"id\":\"https://www.googleapis.com/analytics/v3/data/ga?ids=ga:76546294&dimensions=ga:userType&metrics=ga:users&start-date=2014-10-01&end-date=2014-10-23&max-results=10\",\"query\":{\"start-date\":\"2014-10-01\",\"end-date\":\"2014-10-23\",\"ids\":\"ga:76546294\",\"dimensions\":\"ga:userType\",\"metrics\":[\"ga:users\"],\"start-index\":1,\"max-results\":10},\"itemsPerPage\":10,\"totalResults\":2,\"selfLink\":\"https://www.googleapis.com/analytics/v3/data/ga?ids=ga:76546294&dimensions=ga:userType&metrics=ga:users&start-date=2014-10-01&end-date=2014-10-23&max-results=10\",\"profileInfo\":{\"profileId\":\"76546294\",\"accountId\":\"289147\",\"webPropertyId\":\"UA-289147-1\",\"internalWebPropertyId\":\"456104\",\"profileName\":\"US - Institutional Investors - NP Microsite\",\"tableId\":\"ga:76546294\"},\"containsSampledData\":false,\"columnHeaders\":[{\"name\":\"ga:userType\",\"columnType\":\"DIMENSION\",\"dataType\":\"STRING\"},{\"name\":\"ga:users\",\"columnType\":\"METRIC\",\"dataType\":\"INTEGER\"}],\"totalsForAllResults\":{\"ga:users\":\"1110\"},\"rows\":[[\"New Visitor\",\"826\"],[\"Returning Visitor\",\"284\"]]}</string>",
   "status":200,
   "statusText":"OK"
}

-----------End of JSON ------------

I want to remove these characters from the beginning of the string:

`{"readyState":4,"responseText":"<?xml version=\"1.0\"encoding=\"utf-8\"?>\r\n<string>`

And I want to remove these character from the end of the string:

`</string>","status":200,"statusText":"OK"}`

So I want to remove these characters. I think a set of JavaScript String functions to be used. But I don't know how to mix them and use.

Could someone help me to solve this matter?

Thanks and regards, Chiranthaka

UPDATE

I have used the follwoing AJAX function to send and get the JSON response back.

function setJsonSer() {
                formData = {
                'Email': '[email protected]',
                'Password': 'password1234',
                'URL': getVaria()
            };
                $.ajax({
                url: "/APIWebService.asmx/AnalyticsDataShowWithPost",
                type: 'POST',
                data: formData,
                complete: function(data) {
            var jsonResult = JSON.stringify(data);
            alert(JSON.stringify(data));

            Load(data);

                }
            }); 
    }

UPDATE 02

function setJsonSer() {
                formData = {
                'Email': '[email protected]',
                'Password': 'russell1234',
                'URL': getVaria()
            };
                $.ajax({
                url: "/APIWebService.asmx/AnalyticsDataShowWithPost",
                type: 'POST',
                data: formData,
            dataType: 'json',
                complete: function(data) {
            var jsonResult = JSON.stringify(data);

            alert(jsonResult);

            Load(data);

                }
            });                 

    }

Upvotes: 0

Views: 7608

Answers (2)

Teddy
Teddy

Reputation: 4223

  1. You have to parse JSON to get stuff which is inside it (You have done this)
  2. You have to parse the XML to get text which is inside the XML

Here's sample code for XML parsing: http://www.w3schools.com/xml/tryit.asp?filename=tryxml_parsertest2

Upvotes: 0

Andi N. Dirgantara
Andi N. Dirgantara

Reputation: 2051

I looked at your code:

complete: function(data) {
  var jsonResult = JSON.stringify(data);

  alert(jsonResult);

  Load(data);
}

So you want to stringify your customized result, but your result is not well parsed JSON*? If yes then:

complete: function(data) {
  var responseText = data.responseText;
  var responseJson = JSON.parse(responseText.match(/[{].*.[}]/));
  // you can skip `JSON.parse` if you dont want to leave it as `String` type

  alert(JSON.stringify(responseJson)); //or just `responseJson` if you skip `JSON.parse`

  Load(JSON.stringify(responseJson));
}

This can solve your problem for a while. But I think the problem is in your backend which did not serve well parsed JSON data. My recommendation is fixing your backend system first.

*Not well parsed JSON because your result some kind of including XML type of string under JSON object.

Upvotes: 2

Related Questions