c# - JsonConvert.DeserializeObject throw exception when retrieving JSON from Rest API -
i have issue cannot solve. have nodejs server can send simple json through rest api.
var filename = 'latestversion.json'; fs.readfile(filename, 'utf8', function (err, data) { if (err) throw err; filejson = json.parse(data.tostring('utf8').replace(/^\ufeff/, '')); res.json(data); });
i retrieve json in c# application:
class appversion { public int major { get; set; } public int minor { get; set; } public int build { get; set; } } httpwebrequest http = (httpwebrequest)httpwebrequest.create(new uri(url)); httpwebresponse response = (httpwebresponse)http.getresponse(); string versionstring = null; using(system.io.streamreader sr = new system.io.streamreader(response.getresponsestream())) { versionstring = sr.readtoend(); } if(versionstring != null) { appversion test = new appversion() { major = 1, minor = 1, build = 1 }; string teststr = jsonconvert.serializeobject(test); var j = jsonconvert.deserializeobject<appversion>(versionstring); }
i have exception "cannot cast or convert...." when deserializing. however, if use teststr
goes well. looked @ strings , found :
teststr = "{\"major\":1,\"minor\":1,\"build\":1}" versionstring = "\"{\\\"major\\\": 1, \\\"minor\\\": 1, \\\"build\\\": 1}\""
more strange, tried edit versionstring
hand removing escape characters , got still error (the json visualizer says badly formatted).
any clue solve ?
Comments
Post a Comment