简体   繁体   中英

CheckBox, Multiple Items to be added over Json Writer by reading selected items from Checkbox

I have a Checkbox with 7 different options that can be checked and used, amonst them things like range_close and component_type_stock, component_type_lostech.

I am now trying to write them via a json writer into a json file but if I use

writer.WritePropertyName("ComponentTags");
writer.WriteStartObject();
writer.WritePropertyName("items"); 
writer.WriteRawValue(ComponentTagsCheckBox.CheckedItems.ToString());

All I get is

"ComponentTags": {
"items": System.Windows.Forms.CheckedListBox+CheckedItemCollection,

When I have the range_extreme and component_type_lostech checkboxes marked in the checkbox.

I already tried using a for loop by replacing the last line in the previous code snippet

string s = "";
for (int x = 0; x <= ComponentTagsCheckBox.CheckedItems.Count - 1; x++)
            {
s = s + ComponentTagsCheckBox.CheckedItems[x].ToString();  //+ "\n";
                               }
 writer.WriteRawValue("["+ s +"]");

Here the output is at least

"ComponentTags": {
"items": [component_type_lostechrange_extreme],
"tagSetSourceFile": ""

}

But the output should be this

"ComponentTags" : {
"items" : [
"component_type_variant",
"component_type_variant2",
  "range_extreme"
],
"tagSetSourceFile" : ""

I can do without it looking like the last snippet as long as the components are seperated by a semicolon and surrounded by "".

I also had an idea to split the string into an array and then seperate it again but that seems like a tad bit to much work.

You should change the last line as follows:

writer.WriteStartArray();
foreach (var item in ComponentTagsCheckBox.CheckedItems)
{
    writer.WriteValue(item.ToString());
}

writer.WriteEnd();

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