Welcome Guest, Not a member yet? Register   Sign In
Error in splitting data from Ajax JSON format
#1

[eluser]Unknown[/eluser]
I'm a mobile application developer , we need an Technical Help in JSON (JavaScript Object Notation) , Here we need to get data from server in JSON format . we can able to view the response from server using Firebug ..
But we find the Error (Undefined ) while splitting the JSON format array to individual String .but we can able to split from local workspace .we worked varied method to solve it. we need a guidance to that method .

For Example :

sample Json Format from http://qa-find.uglii.com/Home/GetCou...8c31b6a84.json

[{"i":"W07O5auPxEuqPchXazzI_g","n":"Afghanistan"},{"i":"IjdD3Z-fO0uACOVRhyUBmw","n":"Aland Islands"},..... etc ]

Response in JSON format as Above , we need to split into individual String such as
var country1 = Afghanistan ;
var country2 = Aland Islands;

my code is
----------------------------------------------------------------------------------
var url = "js/test.json";
//var url = "http://qa-find.uglii.com/Home/GetCountries/9a3d0a019-e523-487f-8250-fd98c31b6a84.json";
//Actual Json Url link is url = "http://qa-find.uglii.com/Home/GetCountries/9a3d0a019-e523-487f-8250-fd98c31b6a84.json"
// sample Json Url Link is url = "js/test.json"

$.ajax({
url: url,
contentType: "application/json; charset=utf-8",
dataType: "script",
type: "GET",
cache: false,
processData: false,
error: function (xhtr, response, e) {},
success: function (jsonData, status) {
var jo = eval(jsonData);

// error at jo.length as undefined

for (var i = 0; i < jo.length; i++) {

$("#addoptions").append('<option name='+jo[i].i+>'+jo[i].n+'</option>');

}
}
});
----------------------------------------------------------------------------------------
#2

[eluser]vitoco[/eluser]
You don't need to "eval" the response, cause if you specify json as dataType it return a json object

from jquery $.ajax man page
Quote:dataTypeString
Default: Intelligent Guess (xml, json, script, or html)
The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string). The available types (and the result passed as the first argument to your success callback) are:
....
....
"json": Evaluates the response as JSON and returns a JavaScript object. In jQuery 1.4 the JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. (See json.org for more information on proper JSON formatting.)

Code:
$.ajax({
url: url,
contentType: "application/json; charset=utf-8",
dataType: "json", // <-- JSON RESPONSE
type: "GET",
cache: false,
processData: false,
error: function (xhtr, response, e) {},
success: function (jsonData, status)
{
  // var jo = eval(jsonData); <-- THIS IS NOT NECESSARY , ALREADY A JSON OBJECT
  
  for( oindex in jsonData )
  {
   var obj = jsonData[ oindex ];
   //
   $("#addoptions").append('<option name="'+obj.i+'"&gt;'+obj.n+'</option>');
  }
}
});

Saludos




Theme © iAndrew 2016 - Forum software by © MyBB