我正在使用下面的代码进行序列化。
var json = JsonConvert.SerializeObject(new { summary = summary });
Run Code Online (Sandbox Code Playgroud)
summary是类型的自定义对象SplunkDataModel:
public class SplunkDataModel
{
public SplunkDataModel() {}
public string Category { get; set; }
public int FailureCount { get; set; }
public Dictionary<string, SplunkError> FailureEntity { get; set; }
public Dictionary<string, string> JobInfo { get; set; }
public string JobStatus { get; set; }
public int SuccessCount { get; set; }
public List<string> SuccessEntity { get; set; }
public int TotalCount { get; set; }
} …Run Code Online (Sandbox Code Playgroud) 我的目标是序列化没有任何属性的属性和具有特定自定义属性的属性。
对于以下课程:
public class Msg
{
public long Id { get; set; }
[CustomAttributeA]
public string Text { get; set; }
[CustomAttributeB]
public string Status { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
当我调用一个方法时Serialize(object, CustomAttributeA),我想要以下输出:
{
"Id" : someId,
"Text" : "some text"
}
Run Code Online (Sandbox Code Playgroud)
当我打电话时Serialize(object, CustomAttributeB),我想拥有以下内容:
{
"Id" : someId,
"Status" : "some status"
}
Run Code Online (Sandbox Code Playgroud)
我读过可以通过创建一个自定义来实现这一点ContractResolver,但是在这种情况下,我必须创建两个单独的合同解析器吗?
有人知道序列化对象时是否有可能将类中的某些值转换为Base64吗?我需要一种标记属性的方法,以指示它需要导出为Base64。例如:
using Newtonsoft.Json;
public class MyFoo {
public string Value1 { get; set; }
[ExportThisValueAsBase64]
public string Value2 { get; set; }
}
public void WriteJSON(MyFoo myFoo) {
var contentsToWriteToFile = SerializeObject(myFoo, Formatting.Indented);
}
Run Code Online (Sandbox Code Playgroud)
预期的输出将是:
{ "Value1": "A String", "Value2": base64encodedvalue }
Run Code Online (Sandbox Code Playgroud)
我还需要一种将值从base64读回到类中的string属性的方法。
我正在从事 Xamarin 项目。基本上,我想从 API 接收数据并显示它。为此,我正在使用 RestSharp。
这是我的 API 请求代码。
string test;
test = "yAHO0SsAmjJi1qTZGcK3sMHHIhWTN4Yq";
string s = string.Format("http://192.168.1.4:3116/api/user/getuser/{0}", test);
client = new RestClient(s);
request = new RestRequest(Method.GET);
request.AddHeader("Cache-Control", "no-cache");
request.AddHeader("Content-Type", "application/json");
IRestResponse response2 = client.Execute(request);
Run Code Online (Sandbox Code Playgroud)
这是我收到的 JSON 对象。
{
"id": 1,
"token": "yAHO0SsAmjJi1qTZGcK3sMHHIhWTN4Yq",
"email": "some email",
"password": "testpassword",
"currentCompany": "some company",
"currentRole": "Software Developer",
"date": "Something",
"name": "Some name",
"lastName": "Some surname",
"headLine": "Some text",
"education": "University of Hotshots",
"country": "Who cares",
"imageLocation": "Some url"
}
Run Code Online (Sandbox Code Playgroud)
这是我使用网站为它创建的类:json2csharp.com/
class User
{ …Run Code Online (Sandbox Code Playgroud) 在使用 json.net 时,我需要调整(反)序列化器设置,因此我创建一个JsonSerializerSettings实例并按如下方式使用它:
JsonConvert.DeserializeObject<SomeType>(someString, mySerializerSettings.ss);
Run Code Online (Sandbox Code Playgroud)
但后来我发现有必要对一些损坏的数据使用自定义JsonConverter,然后像这样使用它:
JsonConvert.DeserializeObject<SomeType>(someString, new myConverter());
Run Code Online (Sandbox Code Playgroud)
但我现在发现我需要同时使用JsonSerializerSettings自定义转换器,但我没有看到任何DeserializeObject允许这样做的方法重载。
我可能会缺少什么?
我正在使用 .net v4.5.2 和 json.net v10.0。
我在使用 C#(VS 2017,.Net 4.6)代码时遇到了一些麻烦。如果有人能帮忙就好了。我有一个 JSON 文件:
{
"makerCommission": 10,
"takerCommission": 10,
"buyerCommission": 0,
"updateTime": 1540015045989,
"balances": [
{
"asset": "BTC",
"free": "0.22222222",
"locked": "0.00000000"
},
{
"asset": "LTC",
"free": "2.00000000",
"locked": "3.00000000"
},
{
"asset": "ETH",
"free": "4.00000000",
"locked": "5.00000000"
}
]
}
Run Code Online (Sandbox Code Playgroud)
我想使用以下方法检索任何硬币的“免费”价值:
result = (dynamic)JArray.Parse(MyData)
Run Code Online (Sandbox Code Playgroud)
我不想检索所有免费值。如果我选择 BTC,我怎样才能得到 0.22222222?
我正在开发服务器端 Blazor 应用程序,并且在返回以下 JSON 的特定 API 调用时遇到问题。
{
"id": 2,
"status": 1,
"fileName": "Test PDF",
"fileType": "PDF",
"fileBytes": "",
"allSchemes": false,
"dateModified": "2020-06-12T12:32:08.99",
"dateCreated": "2020-06-11T11:32:19.877",
"isNew": false,
"schemes": [
{
"schemeCode": "0185",
"schemeName": null,
"baseCurrency": null
},
{
"schemeCode": "0186",
"schemeName": null,
"baseCurrency": null
}
]
}
Run Code Online (Sandbox Code Playgroud)
但是,当我使用下面的代码反序列化时,方案列表始终为 0。其他详细信息按预期填充
var accessToken = await _apiService.RequestNewToken(_httpClient);
_httpClient.SetBearerToken(accessToken);
return await JsonSerializer.DeserializeAsync<SchemeDocument>
(await _httpClient.GetStreamAsync($"api/schemeDocument/{id}"), new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
Run Code Online (Sandbox Code Playgroud)
SchemeDocument 类是
public class SchemeDocument
{
public int Id { get; set; } …Run Code Online (Sandbox Code Playgroud) 我有一个实体类,如下所示。我正在使用 Json.Net 将其序列化为 JSON。现在,类中有几个字段/属性需要使用与实际属性不同的名称进行序列化,并且使用属性可以轻松实现这一点,[JsonProperty]如下面的代码所示。但是,如果我需要更改标有属性的主实体本身的名称怎么办[JsonObject]?(这里我谈论的UserDashboards是派生自 的类EntityBase<int>。)我尝试添加一些命名参数,如标题、id 等,但它们没有帮助。
[JsonObject]
public class UserDashboards : EntityBase<int>
{
public int UserID { get; set; }
public int DashboardID { get; set; }
public int DashboardSequence { get; set; }
public string DashboardTitle { get; set; }
public int PermissionLevelID { get; set; }
[JsonProperty("IsHome")]
public Nullable<bool> IsHomeDashboard { get; set; }
[JsonProperty("IsShared")]
public Nullable<bool> IsSharedDashboard { get; set; }
}
Run Code Online (Sandbox Code Playgroud) 我有一个像这样的递归 JSON 对象,表示查询表达式:
{
"where": {
"operator": "AND",
"left": {
"operator": "=",
"$fieldRef": "requestor",
"value": "@me"
},
"right": {
"operator": "=",
"$fieldRef": "state",
"value": "Closed"
}
}
}
Run Code Online (Sandbox Code Playgroud)
更多对象也可以出现在 JSON 层次结构中。这是一个更复杂的例子:
{
"where": {
"operator": "OR",
"left": {
"operator": "=",
"$fieldRef": "id",
"value": "1234"
},
"right": {
"operator": "OR",
"left": {
"operator": "=",
"$fieldRef": "orgId",
"value": "6757"
},
"right": {
"operator": "AND",
"left": {
"operator": "STARTSWITH",
"$fieldRef": "firstname",
"value": "D"
},
"right": {
"operator": "=",
"$fieldRef": "state",
"value": "Closed"
} …Run Code Online (Sandbox Code Playgroud) 我想强制 Newtonsoft.JsonJsonConvert.SerializeObject始终将 bool 值威胁为字符串:"true"和"false"。
我知道这JsonConvert是一个静态类,我可能应该编写一个扩展方法,如下所示:
JsonConvert.DefaultSettings = () => new JsonSerializerSettings {}
Run Code Online (Sandbox Code Playgroud)
但说实话,我不知道该怎么做,也不知道如何将其实现到我的方法中:
public static string Serialize(Foo fooData)
{
return JsonConvert.SerializeObject(fooData);
}
Run Code Online (Sandbox Code Playgroud)
我将不胜感激任何建议。