JSON RPC Serializing Lists of Objects in c#
-
31-05-2021 - |
سؤال
So first some info about the project; here is a class I created :
public class SendOverview
{
public string id { get; set;}
public string method { get; set;}
public SendOV Params {get; set;}
}
public class SendOV
{
public string overviewID { get; set; }
public string overviewType { get; set; }
public string AORParams { get; set; }
public SentDatas arrOptions { get; set; }
}
public class SentDatas
{
public string columnInfo { get; set; }
public string orderInfo { get; set; }
}
A pretty simple class where I want to serialize the whole thing (So, the SendOverview class) by creating an object as done here :
SendOverview test1 = new SendOverview();
test1.id = "1";
test1.method = "getOverviewInfo";
SendOV testOV = new SendOV();
testOV.AORParams = null;
testOV.overviewID = tempDID;
testOV.overviewType = "Stock Items";
SentDatas col1 = new SentDatas();
col1.columnInfo = "1;100;1;1#";
col1.orderInfo = "1;0;0#";
Now once I try to add the col1 data to testOV's arrOptions I get a nullreference exception which blocks my work from any progress.. I have tried much, to no avail.
testOV.arrOptions[0] = col1;
is giving me the exception; Any help is highly appreciated..
(I know I have to create a List[] xx = new List[MAX] somewhere but I'm not able to implement it.)
COMPLIMENTARY QUESTION :
when sending the json string : {\"id\":\"1\",\"method\":\"getOverviewInfo\",\"Params\":{\"overviewID\":\"0000004297\",\"overviewType\":\"Stock Items\",\"AORParams\":null,\"arrOptions\":{\"columnInfo\":\"1;100;1;1#\",\"orderInfo\":\"1;0;0#\"}}}"
All the named parameters should only have the value, not the named parameter; adjusted :
{\"id\":\"1\",\"method\":\"getOverviewInfo\",\"Params\":{"0000004297\","Stock Items",null,{\"columnInfo\":\"1;100;1;1#\",\"orderInfo\":\"1;0;0#\"}}}"
Which JSON property should I add to get this effect? Thank you!
المحلول
I am not entirely sure I understand what it is you are after, but take a look at the following and see if I am on the right track.
Update your class like so:
public class SendOV
{
public string overviewID { get; set; }
public string overviewType { get; set; }
public string AORParams { get; set; }
public List<SentDatas> arrOptions { get; set; }
}
And then update your creation code to this:
SendOverview test1 = new SendOverview();
test1.id = "1";
test1.method = "getOverviewInfo";
SendOV testOV = new SendOV();
testOV.AORParams = null;
testOV.overviewID = tempDID;
testOV.overviewType = "Stock Items";
List<SentDatas> sentDatasList = new List<SentDatas>();
SentDatas col1 = new SentDatas();
col1.columnInfo = "1;100;1;1#";
col1.orderInfo = "1;0;0#";
sentDatasList.Add(col1);
testOV.arrOptions = sentDatasList;