c# - json.net deserialize string to class -


i created json file on server im using send data c# program through json.net deserialize. im im getting null object exception, can please show me how create classes. class here

using system; using system.collections.generic; using system.componentmodel; using system.data; using system.drawing; using system.linq; using system.text; using system.windows.forms; using newtonsoft.json;  namespace windowsformsapplication4 { public partial class form1 : form {     public form1()     {         initializecomponent();         load();     }     public void load()     {         label1.text = "state:\nloading...";         try         {             products pd = new products();             using (var webclient = new system.net.webclient())             {                 // download json url                 var json = webclient.downloadstring(url);                 // parse json.net                 products convert = jsonconvert.deserializeobject<products>(json) products;                 label1.text += pd.info.tostring();                 label1.text += "\nweb service connected to";             }         }         catch (jsonserializationexception jsonerr) { label1.text += "\nweb service connection failed"; messagebox.show(jsonerr.tostring()); }         catch (exception err) { throw; }         { label1.text += "\nweb service closed"; }     } } 

}

public class products {     public info info;      [jsonproperty("post")]     public info infos     {         { return info; }         set { info = value; }     } }  public class info {     private string pd_name;     private int pd_id;      [jsonproperty("pd_id")]     public int pd_ids     {         { return pd_id; }         set { pd_id = value; }     }      [jsonproperty("pd_name")]     public string pd_names     {         { return pd_name; }         set { pd_name = value; }     } } 

you're not handling posts value in json. if json formatted this:

{ "posts" : [ { "post" : { "pd_id" : "399",                             "pd_name" : "1.2mm  cylinder labret"} },               { "post" : { "pd_id" : "415",                             "pd_name" : "1.2mm  laser etched labret" }}             ] } 

try setting classes this:

public class posts {     public list<products> posts { get; set; } }  public class products {     public list<info> post { get; set; } }  public class info {     public string pd_id { get; set; }     public string pd_name {get; set; } } 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -