gpt4 book ai didi

C# Json 格式的响应

转载 作者:太空宇宙 更新时间:2023-11-03 22:55:11 25 4
gpt4 key购买 nike

我知道我需要一个格式化的 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# Json 格式的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45753620/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com