c# 4.0 - How to parse a simple JSON string to a DataSet with Json.Net -
i have tried can think of try parse json string using json.net library, keep getting error says "additional text after string deserialize". have ideas might going wrong?
here json string have:
{"w_vendor":["914115361","914115361","914115361","914115361","914115361","914115361","914115361","914115361","914115361","914115361","914115361","914115361","914115361","914115361","914115361","914115361","914115361","914115361","914115361","914115361","914115361","914115361","914115361","914115361","914115361","914115361","914115361","914115361","914115361","914115361","914115361","914115361","914115361","914115361","914115361","914115361"]}
here code i'm using try parse it:
var pdfvalue = request["pdfvalue"]; string outputcachelocation = jsonconvert.serializeobject(pdfvalue); dataset dataset = jsonconvert.deserializeobject<dataset>(pdfvalue); datatable datatable = dataset.tables["table1"]; response.write(datatable.rows.count);
does see additional text error refers to? can't see it, , json won't parse. i'm ready give , parse manually myself.
instead of deserializing dataset
, have considered creating custom class data , deserializing instead? might little more straightforward work with.
for example, if define class this:
class data { [jsonproperty(propertyname="w_vendor")] public list<string> values { get; set; } }
then can deserialize this:
string json = @"{""w_vendor"":[""914115361"",""914115361"",""914115361"",""914115361"",""914115361"",""914115361"",""914115361"",""914115361"",""914115361"",""914115361"",""914115361"",""914115361"",""914115361"",""914115361"",""914115361"",""914115361"",""914115361"",""914115361"",""914115361"",""914115361"",""914115361"",""914115361"",""914115361"",""914115361"",""914115361"",""914115361"",""914115361"",""914115361"",""914115361"",""914115361"",""914115361"",""914115361"",""914115361"",""914115361"",""914115361"",""914115361""]}"; var data = jsonconvert.deserializeobject<data>(json); response.write(data.values.count);
edit
the reason didn't work way had because data doesn't conform structure need in order deserialize dataset
. if take @ example documentation, data need structured this:
{ "table1" : [ { "column1" : "value1", "column2" : "value2" }, { "column1" : "value3", "column2" : "value4" } ], "table2" : [ { "column1" : "value1", "column2" : "value2" }, { "column1" : "value3", "column2" : "value4" } ] }
specifically, outer object contains properties represent tables. property names correspond names of tables, , values arrays of objects each object represents 1 row in table. objects' properties correspond column names, , values row data.
in data, outer object contains property value array of simple strings, not objects. can't turned table because json.net can't determine column names. also, name of table data have been w_vendor
, not table1
.
Comments
Post a Comment