我知道我需要格式化的Newtonsoft Json响应,但是我不知道该怎么做,而且我似乎找不到可以帮助我的教程或任何类型的文档。

这是我现在拥有的代码。
dtb是我需要以格式化的Newtonsoft Json响应返回的数据库响应

da.Fill(dtb)
return Json(dtb);


这不起作用,当我尝试运行下面的很多代码时,我得到了一个错误

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)
{


我不确定为什么它会在此错误的第一行中断

+       $exception  {"Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1."}   Newtonsoft.Json.JsonReaderException


错误编辑1:

Newtonsoft.Json.JsonSerializationException: 'Cannot deserialize the current JSON array (e.g. [1,2,3]) into type 'AuthorizationServer.api.Models.UserModel' because the type requires a JSON object (e.g. {"name":"value"}) to deserialize correctly.

To fix this error either change the JSON to a JSON object (e.g. {"name":"value"}) or change the deserialized type to an array or a type that implements a collection interface (e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.


我的代码现在看起来像这样

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)


但这使我误以为UserModel m = (UserModel)JsonConvert.DeserializeObject<UserModel>(res);

Microsoft.CSharp.RuntimeBinder.RuntimeBinderException occurred
HResult=0x80131500
Message=The best overloaded method match for 'Newtonsoft.Json.JsonConvert.DeserializeObject<AuthorizationServer.api.Models.UserModel>(string)' has some invalid arguments
Source=AuthorizationServer.api
StackTrace:
at AuthorizationServer.api.Providers.CustomOAuthProvider.GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context) in C:\Users\wilsona\Documents\Visual Studio 2017\Projects\JsonWebTokensWebApi\AuthorizationServer.api\Providers\CustomOAuthProvider.cs:line 66
at Microsoft.Owin.Security.OAuth.OAuthAuthorizationServerHandler.<InvokeTokenEndpointResourceOwnerPasswordCredentialsGrantAsync>d__3f.MoveNext()


用户模型类

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返回

[{"UserID":1,"Username":"andy","PasswordHash":"$2a$10$8PJOsziVcElM6pi9pF8DiuSoE8JS14co6XBjGMITwoZOAPhCmOhK","Roles":"admin"}]


然后通过dynamic res = JsonConvert.DeserializeObject<Dictionary<string, string>>((Newtonsoft.Json.Linq.JToken.Parse(response)[0]).ToString());

击中UserModel m = (UserModel)JsonConvert.DeserializeObject<UserModel>(res);时,哪个工作就会中断

出现此错误Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: 'The best overloaded method match for 'Newtonsoft.Json.JsonConvert.DeserializeObject<AuthorizationServer.api.Models.UserModel>(string)' has some invalid arguments'

最佳答案

问题是Json作为数组发送。您必须将其反序列化为数组。

该代码对我有用。

var jToken = JToken.Parse(response);
var users = jToken.ToObject<List<UserModel>>(); //Converts the Json to a List<Usermodel>
var user = users[0];

关于c# - C#Json格式的响应,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45753620/

10-11 02:39