2

I have this webservice which return a JSON array

 public string getLastDP(string accountID)
 {
    string connectionString = ConfigurationSettings.AppSettings["connectionString"].ToString();

    MySqlConnection connectionObj = new MySqlConnection(connectionString);
    List<lastDPElements> listOFElements = new List<lastDPElements>();
    try
    {
        connectionObj.Open();

        MySqlCommand cmd = new MySqlCommand("REPORTSERVER_ALL_LAST_DP_OF_DEVICE", connectionObj);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add(new MySqlParameter("acc", accountID));
        MySqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
        while (dr.Read())
        {
            lastDPElements temp = new lastDPElements();
            temp.accountID = dr["accountID"].ToString();
            temp.address = dr["address"].ToString();
            temp.deviceID = dr["deviceID"].ToString();
            temp.displayName = dr["displayName"].ToString();
            temp.heading = double.Parse(dr["heading"].ToString());
            temp.inputMask = dr["inputMask"].ToString();
            temp.latitude = double.Parse(dr["latitude"].ToString());
            temp.longitude = double.Parse(dr["longitude"].ToString());
            temp.timestamp = Convert.ToDateTime(dr["timestamp"].ToString());
            listOFElements.Add(temp);
        }
        return new JavaScriptSerializer().Serialize(listOFElements);
    }
    catch
    {
        throw;
    }
    finally
    {
        connectionObj.Close();
        connectionObj.Dispose();
    }



}

The array returned by the service is

[{  "accountID":"rbTours",
"deviceID":"000020120504004",
"timestamp":"\/Date(1359720917000)\/",
"latitude":28.4337833,
"longitude":77.1048833,
"speedKPH":0,
"heading":130.44,
"altitude":0,
"address":"HCG CNG Station, Sector Road to Sector 56, DLF Ph. V, Gurgoan, Gurgaon, Haryana, 122002, India",
"odometerKM":null,
"displayName":"DL 1YC 7276",
"pushPinPath":null,
"temperature":null,
"inputMask":"0"},{"accountID":"rbTours",
"deviceID":"000020120711002",
"timestamp":"\/Date(1358638851000)\/",
"latitude":28.610895,
"longitude":77.3269683,
"speedKPH":0,
"heading":253,
"altitude":0,
"address":"Gazipur Shamshan Ghat, Mayur Vihar III, Patparganj, East Delhi, National Capital Territory of Delhi, 110092, India",
"odometerKM":null,
"displayName":"DL1YC8576",
"pushPinPath":null,
"temperature":null,
"inputMask":"0"},{"accountID":"rbTours",
"deviceID":"000020120618001",
"timestamp":"\/Date(1359721995000)\/",
"latitude":28.6345933,
"longitude":77.449505,
"speedKPH":0,
"heading":284.04,
"altitude":0,
"address":"National Highway 24 Bypass, B-Block, Lohia Nagar, Ghaziabad, Ghaziabad, Ghaziabad, Uttar Pradesh, 201001, India",
"odometerKM":null,
"displayName":"DL 1Y C6322",
"pushPinPath":null,
"temperature":null,
"inputMask":"0"},{"accountID":"rbTours",
"deviceID":"000020120618002",
"timestamp":"\/Date(1359715788000)\/",
"latitude":28.4446867,
"longitude":77.0450667,
"speedKPH":0,
"heading":35.25,
"altitude":0,
"address":"NH8, Sec 38, Gurgoan, Gurgaon, Haryana, 122018, India",
"odometerKM":null,
"displayName":"UP16 AT 7410",
"pushPinPath":null,
"temperature":null,
"inputMask":"0"},{"accountID":"rbTours",
"deviceID":"000020120711001",
"timestamp":"\/Date(1359693209000)\/",
"latitude":28.5384833,
"longitude":77.1915783,
"speedKPH":0,
"heading":288.26,
"altitude":0,
"address":"Aurobindo Marg, Qutab Institutional Area, Katwaria Sarai, South Delhi, National Capital Territory of Delhi, 110016, India",
"odometerKM":null,
"displayName":"UP16 AT7415",
"pushPinPath":null,
"temperature":null,
"inputMask":"0"},{"accountID":"rbTours",
"deviceID":"356307040485566",
"timestamp":"\/Date(1359698595000)\/",
"latitude":28.5716512,
"longitude":77.136544,
"speedKPH":0,
"heading":0,
"altitude":0,
"address":"NH8, Palam, New Delhi, National Capital Territory of Delhi, 110021, India",
"odometerKM":null,
"displayName":"DL 1Y C9174",
"pushPinPath":null,
"temperature":null,
"inputMask":"0"},{"accountID":"rbTours",
"deviceID":"000020120707001",
"timestamp":"\/Date(1359722004000)\/",
"latitude":28.6107633,
"longitude":77.327815,
"speedKPH":0,
"heading":299.16,
"altitude":0,
"address":"Sector 11, Noida, Ghaziabad, Ghaziabad, Uttar Pradesh, 201307, India",
"odometerKM":null,
"displayName":"DL 1YC 8577",
"pushPinPath":null,
"temperature":null,
"inputMask":"0"},{"accountID":"rbTours",
"deviceID":"000020120623001",
"timestamp":"\/Date(1359722004000)\/",
"latitude":28.6425567,
"longitude":77.1736367,
"speedKPH":0,
"heading":124.77,
"altitude":0,
"address":"Patel Road, Patel Nagar, Kirti nagar, West Delhi, National Capital Territory of Delhi, India",
"odometerKM":null,
"displayName":"UP16 AT 8992",
"pushPinPath":null,
"temperature":null,
"inputMask":"0"},{"accountID":"rbTours",
"deviceID":"000020120619001",
"timestamp":"\/Date(1359721984000)\/",
"latitude":28.43367,
"longitude":77.1048533,
"speedKPH":0,
"heading":154.8,
"altitude":0,
"address":"Centrum Plaza, Sector Road to Sector 56, DLF Ph. V, Gurgoan, Gurgaon, Haryana, 122002, India",
"odometerKM":null,
"displayName":"UP16 AT 9104",
"pushPinPath":null,
"temperature":null,
"inputMask":"0"},{"accountID":"rbTours",
"deviceID":"000020120609005",
"timestamp":"\/Date(1359721996000)\/",
"latitude":28.6760217,
"longitude":77.2942033,
"speedKPH":0,
"heading":92.92,
"altitude":0,
"address":"National Highway 24, Shahdara, North East Delhi, National Capital Territory of Delhi, India",
"odometerKM":null,
"displayName":"DL1V A 8569",
"pushPinPath":null,
"temperature":null,
"inputMask":"0"},{"accountID":"rbTours",
"deviceID":"000020120818004",
"timestamp":"\/Date(1359721960000)\/",
"latitude":27.1598883,
"longitude":78.0428533,
"speedKPH":0,
"heading":1.07,
"altitude":0,
"address":"The Silk Route Restaurant, SH62, Tajganj, Agra, Uttar Pradesh, 282001, India",
"odometerKM":null,
"displayName":"DL 1V A 8743",
"pushPinPath":null,
"temperature":null,
"inputMask":"0"},{"accountID":"rbTours",
"deviceID":"000020120813001",
"timestamp":"\/Date(1359715794000)\/",
"latitude":28.613985,
"longitude":77.331255,
"speedKPH":0,
"heading":231.58,
"altitude":0,
"address":"Sector 11, Noida, Ghaziabad, Ghaziabad, Uttar Pradesh, 201307, India",
"odometerKM":null,
"displayName":"DL 1Y C6321",
"pushPinPath":null,
"temperature":null,
"inputMask":"0"}];

How can I parse it to get data in a format that I can get individual elements. I am calling the service from following script.

function getData() {
            var accountObj = 'rbTours';
            $.ajax({
                url: "Services/GetData.asmx/getLastDP",
                type: "POST",
                dataType: "json",
                data: "{accountID:'" + accountObj + "'}",
                contentType: "application/json; charset=utf-8",
                success: function(response) {
                    functionOnSuccess(response.d);
                },
                error: function(e) {
                    alert(e);
                }
            });
            return false;
        }
        function functionOnSuccess(arrayOfElements) {
        var data=[];
        if (arrayOfElements != null && arrayOfElements != 'undefined') {
        for (var i = 0; i < arrayOfElements.length; i++) {
          var row = new Array();
          row[0] = response[i].latitude;
          row[1] = response[i].longitude;              
          data.addRow(row);
          }
          }
         alert(data);
        }

What should I do on OnSuccess to get elements in an list format I want to get the data like

var requiredValue=array[0].latitude etc

Sorry for my english..

Naveed S
  • 5,106
  • 4
  • 34
  • 52
user2034828
  • 23
  • 1
  • 7

4 Answers4

0

The issue is: you are trying to access response from the function.

Try this:

function functionOnSuccess(arrayOfElements) {
    var data=[];
    if (arrayOfElements != null && arrayOfElements != 'undefined') {
        for (var i = 0; i < arrayOfElements.length; i++) {
            var row = new Array();
            row[0] = arrayOfElements[i].latitude;
            row[1] = arrayOfElements[i].longitude;              
            data.addRow(row);
        }
    }
    alert(data);
}
ATOzTOA
  • 34,814
  • 22
  • 96
  • 117
0

can you post the json response you are getting in the firebug console?

you dont have to parse the json manually, when you set the dataType:'json' jquery parses the response for you and by the look of your while loop you can access the json prop like

success:function(data){
 console.log(data[0].lastDPElements);
}

you can loop through the data and the props like above...

Dakait
  • 2,531
  • 1
  • 25
  • 43
0

You can get the values as follows.

  function getData() {
            var accountObj = 'rbTours';
            $.ajax({
                url: "Services/GetData.asmx/getLastDP",
                type: "POST",
                dataType: "json",
                data: "{accountID:'" + accountObj + "'}",
                contentType: "application/json; charset=utf-8",
                success: function(response) {
                    functionOnSuccess(response.d);
                },
                error: function(e) {
                    alert(e);
                }
            });
            return false;
        }
        function functionOnSuccess(arrayOfElements) {
        var data=[];
        if (arrayOfElements != null && arrayOfElements != 'undefined') {
          requiredValue=arrayOfElements[0].latitude; 
        }

edit 1

or even you can loop through your collection

   function functionOnSuccess(arrayOfElements) {
        var data=[];
        if (arrayOfElements != null && arrayOfElements != 'undefined') {
          for(i=0;i<arrayOfElements.length;i++)
          requiredValue=arrayOfElements[i].latitude; 
        }

edit 2

 function functionOnSuccess(arrayOfElements) {
       obj = JSON.parse(arrayOfElements);
       alert(obj.count);

and see what is there in count.

edit 3

 function functionOnSuccess(arrayOfElements) {
       obj = JSON.parse(arrayOfElements);
       alert(obj.length);

and see what is there in count.

शेखर
  • 17,412
  • 13
  • 61
  • 117
0
you have to try this 

 function getData() {
            var accountObj = 'rbTours';
            $.ajax({
                url: "Services/GetData.asmx/getLastDP",
                type: "POST",
                dataType: "json",
                data: "{accountID:'" + accountObj + "'}",
                contentType: "application/json; charset=utf-8",
                success: function(response) {
                    **functionOnSuccess((response.getLastDPResult).Toarray());**
                },
                error: function(e) {
                    alert(e);
                }
            });
            return false;
        }
Sagar Hirapara
  • 1,677
  • 13
  • 24