Note that there are some explanatory texts on larger screens.

plurals
  1. PONested list with JavaScriptSerializer
    primarykey
    data
    text
    <p>I'm trying to produce the following JSON structure with JavaScriptSerializer in Net 3.5:</p> <pre><code>{ "chart": "pie", "series": [ { "name": "Browsers", "data": [ [ "Firefox", 6 ], [ "MSIE", 4 ] ], "size": "43%" } ] } </code></pre> <p>The issue I face is regarding the <em>data</em> section. I cannot seem to create a class that serializes into the above structure in <em>data</em>.</p> <p>I have read other posts which kind of touch the subject but the solution always seem to be to use JSON.Net. For instance <a href="https://stackoverflow.com/questions/6416950/serializing-dictionaries-with-javascriptserializer">Serializing dictionaries with JavaScriptSerializer</a>. Although, I primaily want to solve it by using the JavaScriptSerializer or other built-in features in the .Net 3.5 framework.</p> <p>How do I produce the data section?</p> <p><strong>*EDIT</strong> The way to solve it with the JavaScriptSerializer is to create an object array for the data property on the object about to be serialized.</p> <p>Input parameter <em>data</em> is "Firefox:6\nMSIE:4\nChrome:7".</p> <p>public object[] GetData(string data) { var dataStrArr = data.Split(new[] { Environment.NewLine }, StringSplitOptions.None);</p> <pre><code> var dataArr = new object[dataStrArr.Length]; for (var index = 0; index &lt; dataStrArr.Length; index++) { var component = dataStrArr[index]; var componentArr = component.Split(':'); if (componentArr.Length != 2) continue; var key = componentArr[0]; decimal value; if (decimal.TryParse(componentArr[1], out value)) { var keyValueArr = new object[2]; keyValueArr[0] = key; keyValueArr[1] = value; dataArr[index] = keyValueArr; } } return dataArr; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload