I have a JSON response from my php upload in a free hosting site that will output the following:
{"cart":[{"cardno":"1111111111","product_id":"10000104","name":"Hotsilog","quantity":"1","price":"35","category":"breakfast"},{"cardno":"1111111111","product_id":"10000107","name":"Champorado","quantity":"1","price":"15","category":"breakfast"}]}
and I have this C# form to test if im deserializing it properly:
public class Costing
{
public List<CustomerCosting> costing { get; set; }
}
public class CustomerCosting
{
public string id { get; set; }
public string cardno { get; set; }
public string product_id { get; set; }
public string name { get; set; }
public string quantity { get; set; }
public string price { get; set; }
public string category { get; set; }
}
private void button3_Click(object sender, EventArgs e)
{
NameValueCollection userInfo = new NameValueCollection();
userInfo.Add("cardno", textBox6.Text);
byte[] uploadVal = client.UploadValues("http://eallowance.x10host.com/v1/getCart.php", "POST", userInfo);
string getResponse = Encoding.ASCII.GetString(uploadVal);
richTextBox1.Text = getResponse.ToString();
Costing customerCosting = JsonConvert.DeserializeObject<Costing>(getResponse);
foreach (var item in customerCosting.costing)
{
listView1.Items.Add("name: {3}", item.name);
}
}
Everything is good up to the richTextBox1.Text = getResponse.ToString();
part. It is there to test if im getting the response properly. But when it enters the foreach part, it crash and gives a NullReferenceException
. The listView1.Items.Add
part is just to test if it got deserialized. What I will do for the foreach part is to upload each row into my database.