简体   繁体   中英

Noob using C# classes and accessing them

I'm working with JSON (using json.net) and a C# console application and I am trying to set some values for a JSON POST to a server.

I can set some of the values, but accessing others is giving me fits.

My JSON looks like this:

{
    "params" : [
         {
             "url" : "sys/login/user",
             "data" : [
                 {
                     "passwd" : "pwd",
                     "user" : "user"
                 }
             ]
         }
    ],
    "session" : 1,
    "id" : 1,
    "method" : "exec"
}

I ran that through json2csharp and it generated me the following classes.

public class Datum
{
    public string passwd { get; set; }
    public string user { get; set; }
}

public class Param
{
    public string url { get; set; }
    public List<Datum> data { get; set; }
}

public class RootObject
{
    public List<Param> @params { get; set; }
    public string session { get; set; }
    public int id { get; set; }
    public string method { get; set; }
}

I then created this object for testing in my Main method

RootObject temp = new RootObject()
temp.id = 1;
temp.method = "exec";
temp.session = "1";

and those parameters get set just fine. I can also set the URL param using the following:

temp.@params.Add(new Param { url = "some/url", });

It is setting the public List<Datum> data { get; set; } public List<Datum> data { get; set; } public List<Datum> data { get; set; } item that is the problem. I cannot figure out how to access that and set the user and password items.

If I add this to the Param class I can set the values, but this seems to be the wrong way/place to me.

public Param()
{
   data = new List<Datum>();            
   data.Add(new Datum { user = "user", passwd = "pass" });
}      

Well, you create your RootObject like this:

RootObject temp = new RootObject()
temp.id = 1;
temp.method = "exec";
temp.session = "1";

Then you create the params list and fill it with one Param :

temp.@params = new List<Param>();
temp.@params.Add(new Param { url = "some/url" });

You can then set the data for one param in the list (in this example the first one):

temp.@params[0].data = new List<Datum>();
temp.@params[0].data.Add(new Datum { user = "user", passwd = "pass" });

This is necessary, because @params is a list of Param objects. You could also fill the data when creating the Param instance before adding it to the list (easier, because you otherwise need to know the list index).

temp.@params = new List<Param>();

Param p = new Param { url = "some/url" };
p.data = new List<Datum>();
p.data.Add(new Datum() { ... });

temp.@params.Add(p);

Usually you'd change change the default constructors to initialize the lists already and prevent the list instances from being replaced by changing the properties to read-only, but that might not work well with JSON deserialization, so you really need to try this. It would look like this:

public class Param
{
    public Param()
    {
        data = new List<Datum>();
    }

    public string url { get; set; }
    public List<Datum> data { get; private set; }
}

public class RootObject
{
    public RootObject()
    {
        @params = new List<Param>();
    }

    public List<Param> @params { get; private set; }
    public string session { get; set; }
    public int id { get; set; }
    public string method { get; set; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM