0

i am just calling my server side method by jquery and my server side method look like

[WebMethod]
public static Dictionary<string,string> GetPayPalData()
{
    Dictionary<string, string> rmPayPal = new Dictionary<string, string>();

    int counter = 0;
    Invoice _selectedInvoice = SelectedInvoice;
    rmPayPal.Add("first_name", "Tridip");
    rmPayPal.Add("last_name", "BBA");


    foreach (InvoiceItem x in _selectedInvoice.InvoiceItems)
    {
        counter++;
        rmPayPal.Add("item_name_" + counter.ToString(), x.ProductName);
        rmPayPal.Add("amount_" + counter.ToString(), x.UnitPrice.ToString("#.00"));
        rmPayPal.Add("quantity_" + counter.ToString(), x.Quantity.ToString());
        rmPayPal.Add("shipping_" + counter.ToString(), (x.ShippingCost * x.Quantity).ToString("#.00"));
        rmPayPal.Add("handling_" + counter.ToString(), (x.HandlingCost * x.Quantity).ToString("#.00"));
    }

     rmPayPal.Add("country", "GB");
     rmPayPal.Add("currency_code", "GBP");
     return rmPayPal;

}

my sample json look like

{"d":
{"first_name":"Tridip",
"last_name":"BBA",
"address1":"Unit 1 Stirling Park",
"address_override":"1",
"city":"Rochester",
"state":"KENT",
"zip":"ME1 3QR",
"cmd":"_cart",
"upload":"1",

"item_name_1":
"Product #1",
"amount_1":"13.00",
"quantity_1":"4",
"shipping_1":"12.00",
"handling_1":"2.00",

"item_name_2":
"Product #3",
"amount_2":"11.00",
"quantity_2":"1",
"shipping_2":"3.00",
"handling_2":".50",

"country":"GB",
"currency_code":"GBP"
}
 }

my server side function is called perfectly by jquery and data in json format is returning to client side fine. my concern is how to parse the above json by jquery.

because first name, last name and few fields will be there in json string only once but Item_Name,amount_,quantity_,shipping_ etc fields could be many in json string. so i have to read those preparative fields in loop which i am not being able to do so. so please help me with small code snippet of jquery for parsing by which i can read those fields which will come once in json and as well as i need to read those fields which will come as many as time in json string. thanks

3 Answers 3

1
  // JavaScript
  function test(data){
     var result = jQuery.parseJSON(data);                                                
     // you can access them like 
     result.first_name ;
     result.last_name ;
  }
Sign up to request clarification or add additional context in comments.

2 Comments

i am getting error....result is always showing null but i have seen that json response is coming by firebug. can u suggest anyother way out to parse json data.
try to print the data before parsing and print it after parsing .... i expected the returned data(JSON) may be invalid
0

You can use jQuery parseJSON http://api.jquery.com/jQuery.parseJSON/ method to parse the json String in to object.

And you can loop through if any by using jQuery .each http://api.jquery.com/jQuery.each/ this can be use to iterate on the object.

2 Comments

In order to make use of jQuery.each() on your response, better make array of items. Otherwise it will be very difficult for you to identify how many items are present.
how to return array of items including with dictionary. can u plzz come with some sample code.
0

If you are getting an error with var result = jQuery.parseJSON(data); it may well be that the data is not correctly formatted. first try to see if you can parse something like:

var JSONstring= "[{\"Type\":2,\"Number\":\"000 000 000\",\"TypeName\":\"mobile\",\"Extension\":\"\",\"ID\":\"a2fc8a78-71a8-4550-9a9f-1c1ad4cab1d9\",\"IsNew\":false,\"IsEdited\":false}]";
var JSONobj = jQuery.parseJSON(JSONstring);
/*the you can iterate through json object's member with a for loop*/
var counter = 0;
for (counter = 0; counter < JSONobj.length; counter++)
   if (number == JSONobj[counter].Number)
      DoSomething(JSONobj[counter].Number);

If you want to add another array of object to the string the correct format would be:

var JSONstring = [{"Name1":value1,"Name2":value2,...,"Namei":valuei},{},...,{}]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.