Hi,
I'm looking for a way to generate data in a specific JSON format to sent it to a unknown web server.
I have two tables (table1 with column1, column2, column3 and table2 with column2, column4). The table1 has a foreign key to table2 (column2).
The JSON should look like:
{
"column1": "xxx",
"table2": {
"column2": 1
},
"column3": "xxx"
}
So the requirements are:
1. the data of the foreign key must be in a extra segment
2. only the primary key of table2 should be part of the extra segment
3. NULLvalues should be ignored
Suggestions, guidance, and samples are welcome and appreciated!
Thanks and regards,
Wilko Lindemann
FYI: Wilko's post refers to a .NET DataStore .. so I "think" that he is referring to a SNAP C# object not PB. Could be wrong here though. Just my guess.
Regards ... Chris
Then I would have a look at json.net from newtonsoft.
A .NET datastore is an Enumerable, and all IEnumerable types can be serialized as json array.
Best,
.m
thanks for your answer.
I've tried 2 things from newtonsoft. "SerializeObject" from "JsonConvert" and the "JsonWriter".
First to the "JsonConvert". It works fine to eliminate NULL values, but now the upcomming problem is that I'm just able to serialize 1 datastore with the following result:
{
"column1": "xxx",
"column2": 1,
"column3": "xxx"
}
1. So how do I can force an extra section into the created json?
2. Do I have to link the secound datastore for table2 with the first one?
3. Is there an equivalent to the modelbuilder in entity framework for .NET Datastores?
Now my question to the JsonWriter:
1. Do I realy have to write "WritePropertyName" and "WriteValue" for every coulmn? Isn't the a more elegant method?
2. Does exits an option for ignoring NULL values similar to the "NullValueHandling" of "JsonSerializerSettings" for the "JsonConvert"? Or do I have to check every value in an if-clause before writing the property?
My Code looks like this:
var dsStore = new DataStore<Ds_Table1>(context);
dsStore.Retrieve();
StringBuilder sb = new StringBuilder();
StringWriter sw = new StringWriter(sb);
using (JsonWriter writer = new JsonTextWriter(sw))
{
writer.Formatting = Formatting.Indented;
for (int i=0;i<dsStore.RowCount;i++)
{
writer.WriteStartObject();
writer.WritePropertyName("column1");
writer.WriteValue(dsStore.GetItem<int>(i,"column1",DwBuffer.Primary));
// extra section for column2
writer.WritePropertyName("table2");
writer.WriteStartObject();
writer.WritePropertyName("column2");
writer.WriteValue(dsStore.GetItem<int>(i,"column2",DwBuffer.Primary));
writer.WriteEnd();
writer.WritePropertyName("column3");
writer.WriteValue(dsStore.GetItem<int>(i,"column3",DwBuffer.Primary));
//... some more columns
writer.WriteEndObject();
}
}
Thanks and regards,
Wilko Lindemann