我想强制 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)
我将不胜感激任何建议。
我有一些 JSON,如下所示。我想从中获取“类型”为“项目”的所有对象。
string json = @"
{
'name': 'Object 1',
'content': {
'body': {
'id': 'body',
'type': 'Body'
},
'style': {
'id': 'style',
'type': 'Style'
},
'DynamicName-123': {
'id': 'DynamicName-123',
'type': 'Row'
},
'DynamicName-434': {
'id': 'DynamicName-434',
'type': 'Column'
},
'DynamicName-223': {
'id': 'DynamicName-223',
'type': 'Item'
}
}
}";
JObject obj = JObject.Parse(json);
Run Code Online (Sandbox Code Playgroud)
预期输出:
'id': 'DynamicName-223',
'type': 'Item'
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
我有(或将有;它尚未编写)一个 .NET Web API 控制器方法,我将要返回一个整数列表作为 JSON 响应。在客户端,我只想将整数反序列化为列表。我尝试使用 { [1,2,3,4] },但这显然不是有效的 JSON。如果我执行 { "ids" : [1,2,3,4] },它不会反序列化为一个简单的列表。我知道我可以为此创建一个超级简单的类,但我希望避免这种情况。有解决方案吗?谢谢!
var ids = JsonConvert.DeserializeObject<List<int>>(jsonResult1);
Run Code Online (Sandbox Code Playgroud) 我尝试反序列化JSON字符串时收到错误:
无法反序列化当前的JSON数组(例如[1,2,3])...
vb.net代码:
Public Class DATA
Public Property CPU As String
End Class
Dim data As DATA = JsonConvert.DeserializeObject(Of DATA)(File.ReadAllText("laptop.json"))
TextBox25.Text = DATA.CPU
Run Code Online (Sandbox Code Playgroud)
JSON文件:
[
{
"spec": "CPU";
"value": "Intel Core i3-4005U"
};
{
"spec": "Speed";
"value": "1.7 GHz"
};
{
"spec": "Cache";
"value": "3MB"
};
{
"spec": "RAM";
"value": "4GB"
};
{
"spec": "Warranty Period";
"value": "1 Year"
}
]
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个错误?
我正在使用 JSON.NET 来解析我的 JSON:
JObject jsonObject = JObject.Parse(myJson);
Run Code Online (Sandbox Code Playgroud)
我的 JSON 看起来像这样:
{
"_links": {
"self": {
"href": "https://api-uat.dwolla.com/accounts/069b759d-6267-42d6-b30b-5d03ddd25673/funding-sources",
"type": "application/vnd.dwolla.v1.hal+json",
"resource-type": "funding-source"
}
},
"_embedded": {
"funding-sources": [
{
"_links": {
"self": {
"href": "https://api-uat.dwolla.com/funding-sources/0b2f6a31-b909-4c7d-a9f8-6253e5f791d0",
"type": "application/vnd.dwolla.v1.hal+json",
"resource-type": "funding-source"
},
"account": {
"href": "https://api-uat.dwolla.com/accounts/069b759d-6267-42d6-b30b-5d03ddd25673",
"type": "application/vnd.dwolla.v1.hal+json",
"resource-type": "account"
},
"balance": {
"href": "https://api-uat.dwolla.com/funding-sources/0b2f6a31-b909-4c7d-a9f8-6253e5f791d0/balance",
"type": "application/vnd.dwolla.v1.hal+json",
"resource-type": "balance"
}
},
"id": "0b2f6a31-b909-4c7d-a9f8-6253e5f791d0",
"status": "verified",
"type": "bank",
"name": "Bank Of America",
"created": "2017-03-22T12:54:51.000Z",
"removed": false,
"channels": [
"ach"
],
"bankName": "SANDBOX TEST …Run Code Online (Sandbox Code Playgroud) "message":{
"type":"text",
"text":"a message to the service",
"media":"http://example.com",
"location":{
"lat":50.76891,
"lon":6.11499
},
"tracking_data":"tracking data"
}
Run Code Online (Sandbox Code Playgroud)
如何获得 Json、lat 和 lon 子元素的值?
JToken message = data.SelectToken("message");
var lat = message.Value<double>("lat");
Run Code Online (Sandbox Code Playgroud)
这显示 0,而不是实际值。
我有一个很大的嵌套 JSON。我不知道 JSON 的结构。我只有 JSON 中存在的一组键,但我不知道 JSON 中的具体位置。假设密钥存在于其中的某个位置,如何从未知的 JSON 结构中找出密钥的路径?
我有一个 JSON 对象:
{
""settings"": {
""general"": {
""database"": { ""type"": ""PostgreSql"" }
}
}
}
Run Code Online (Sandbox Code Playgroud)
我的 JSON 对象的绝对路径如下所示:settings/general/database/type
我试图用这个问题的第一个解决方案获取所有密钥:
IList<string> keys = parent.Properties().Select(p => p.Name).ToList();
Run Code Online (Sandbox Code Playgroud)
这对我不起作用。该keys列表只包含第一个密钥settings,没有其他密钥。
有一个path属性显示您所在节点的路径,但它不显示 JSON 对象的完整路径。
如何获得像我的示例中那样的绝对路径?
我的文件中的行内容格式如下:
Ref.,ID,Firstname,Secondname,City
Run Code Online (Sandbox Code Playgroud)
这是我的文件:
1,4234,Firstname1,Secondname1,City1
2,5647,Firstname2,Secondname2,City2
3,1657,Firstname3,Secondname3,City3
4,3898,Firstname4,Secondname4,City4
Run Code Online (Sandbox Code Playgroud)
现在我想按 ID 对其进行排序,然后将其再次放入文件中,如下所示:
3,1657,Firstname3,Secondname3,City3
4,3898,Firstname4,Secondname4,City4
1,4234,Firstname1,Secondname1,City1
2,5647,Firstname2,Secondname2,City2
Run Code Online (Sandbox Code Playgroud)
注意:整行是一个字符串变量。
那么有没有办法做到这一点?
这是我的代码:
int counter = 0;
string ln; //Each line in file per loop Ex:1,8957,Firstname,lastname,city
using (StreamReader file = new StreamReader(filePath))
{
Console.WriteLine("Records in the file" + Environment.NewLine);
Console.WriteLine("Ref,First name,Second name,City" + Environment.NewLine);
while ((ln = file.ReadLine()) != null)
{
Console.WriteLine(ln + Environment.NewLine);
/*
So here i need to sort the lines in the file, which written hard coded in the file before spliting it. …Run Code Online (Sandbox Code Playgroud)