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
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.