C# Json formatted response -
i know need formatted newtonsoft json
response don't know how , can't seem find tutorial or sort of documentation me out.
this code have right now. dtb response database need return in formatted newtonsoft json
response
da.fill(dtb) return json(dtb);
this doesn't work , i'm getting error when try , run next lot of code
newtonsoft.json.jsonconvert.deserializeobject<dictionary<string, string>>((newtonsoft.json.linq.jobject.parse(response)["d"]).tostring()); usermodel m = (usermodel)jsonconvert.deserializeobject<usermodel>(response); if (m.username == context.username && m.passwordhash == myhash) {
i'm not sure why breaks @ first line of saying error
+ $exception {"error reading jobject jsonreader. current jsonreader item not object: startarray. path '', line 1, position 1."} newtonsoft.json.jsonreaderexception
error edit 1:
newtonsoft.json.jsonserializationexception: 'cannot deserialize current json array (e.g. [1,2,3]) type 'authorizationserver.api.models.usermodel' because type requires json object (e.g. {"name":"value"}) deserialize correctly. fix error either change json json object (e.g. {"name":"value"}) or change deserialized type array or type implements collection interface (e.g. icollection, ilist) list<t> can deserialized json array. jsonarrayattribute can added type force deserialize json array.
my code looks
dynamic res = jsonconvert.deserializeobject<dictionary<string, string>>((newtonsoft.json.linq.jtoken.parse(response)[0]).tostring()); usermodel m = (usermodel)jsonconvert.deserializeobject<usermodel>(res); if (m.username == context.username && m.passwordhash == myhash)
but gives me error @ usermodel m = (usermodel)jsonconvert.deserializeobject<usermodel>(res);
saying
microsoft.csharp.runtimebinder.runtimebinderexception occurred hresult=0x80131500 message=the best overloaded method match 'newtonsoft.json.jsonconvert.deserializeobject<authorizationserver.api.models.usermodel>(string)' has invalid arguments source=authorizationserver.api stacktrace: @ authorizationserver.api.providers.customoauthprovider.grantresourceownercredentials(oauthgrantresourceownercredentialscontext context) in c:\users\wilsona\documents\visual studio 2017\projects\jsonwebtokenswebapi\authorizationserver.api\providers\customoauthprovider.cs:line 66 @ microsoft.owin.security.oauth.oauthauthorizationserverhandler.<invoketokenendpointresourceownerpasswordcredentialsgrantasync>d__3f.movenext()
user model class
using system; using system.collections.generic; using system.linq; using system.web; namespace authorizationserver.api.models { public class usermodel { public string username { get; set; } public string passwordhash { get; set; } public int userid { get; set; } public string roles { get; set; } public usermodel(string username, string passwordhash, int userid, string roles) { this.username = username; this.passwordhash = passwordhash; this.userid = userid; this.roles = roles; } } }
json return
[{"userid":1,"username":"andy","passwordhash":"$2a$10$8pjoszivcelm6pi9pf8diusoe8js14co6xbjgmitwozoaphcmohk","roles":"admin"}]
which gets passed through dynamic res = jsonconvert.deserializeobject<dictionary<string, string>>((newtonsoft.json.linq.jtoken.parse(response)[0]).tostring());
which works breaks when hits usermodel m = (usermodel)jsonconvert.deserializeobject<usermodel>(res);
with error microsoft.csharp.runtimebinder.runtimebinderexception: 'the best overloaded method match 'newtonsoft.json.jsonconvert.deserializeobject<authorizationserver.api.models.usermodel>(string)' has invalid arguments'
the problem json sent array. have deserialize array.
this code works me.
var jtoken = jtoken.parse(response); var users = jtoken.toobject<list<usermodel>>(); //converts json list<usermodel> var user = users[0];
Comments
Post a Comment