对于 Dynamo 的分页响应,我试图保留该ExclusiveStartKey
值。在代码示例中,如果我response.LastEvaluatedKey
直接使用该值,后续请求可以正常工作。
但是,如果我序列化response.LastEvaluatedKey
然后将其序列化回用作值ExclusiveStartKey
,则后续请求将失败并显示以下错误消息:
提供的起始键无效:一个或多个参数值无效:空属性值类型必须具有 true 值
反序列化的字典似乎与原始字典具有相同的值...是否需要检查两者之间的差异?
QueryResponse response = null;
do
{
string gsiPartitionKey = "gsi-pk-value-1";
var queryRequest = new QueryRequest()
{
TableName = "my-table",
IndexName = "my-index",
KeyConditionExpression = "IndexPk = :s_gsiPartitionKey",
ExpressionAttributeValues = new Dictionary<string, AttributeValue>
{
{
":s_gsiPartitionKey", new AttributeValue { S = gsiPartitionKey}
}
},
Limit = 1
};
if (response != null)
{
//OPTION 1 - OK - Using LastEvaluatedKey directly works fine
//queryRequest.ExclusiveStartKey = …
Run Code Online (Sandbox Code Playgroud) 我有以下字典,我非常想使用 Json.Net 序列化它。字典包含IConvertible
接口的项目,允许我将所需的任何原始类型添加到字典中。
var dic = new Dictionary<string, IConvertible>();
dic.Add("bool2", false);
dic.Add("int2", 235);
dic.Add("string2", "hellohello");
Run Code Online (Sandbox Code Playgroud)
我有以下使用 Json.net 序列化列表的实现:
var settings = new JsonSerializerSettings();
settings.TypeNameHandling = TypeNameHandling.Objects;
var dicString = JsonConvert.SerializeObject(dic, Newtonsoft.Json.Formatting.Indented, settings);
Run Code Online (Sandbox Code Playgroud)
这给了我以下输出:
{
"$type": "System.Collections.Generic.Dictionary`2[[System.String, mscorlib],[System.IConvertible, mscorlib]], mscorlib",
"bool2": false,
"int2": 235,
"string2": "hellohello"
}
Run Code Online (Sandbox Code Playgroud)
然而。当尝试这样反序列化时:
var dic2 = JsonConvert.DeserializeObject<Dictionary<string, IConvertible>>(dicString);
Run Code Online (Sandbox Code Playgroud)
...我收到以下错误:
Error converting value False to type 'System.IConvertible'. Path 'bool2', line 3, position 16.
Run Code Online (Sandbox Code Playgroud)
我环顾四周,发现了以下内容;但设置 typeNameHandling 并没有解决问题。我也不能用IConvertible
类型名称属性来装饰该值,因为它是一个字典。
我还没有找到有关该主题的任何其他信息,因此我们将不胜感激!
我也找到了这个解决方案,但它涉及创建一个 ExpandableObjectConverter 这不是一个非常优雅的解决方案。 …
我有一个自定义的 ContracteResolver,并且得到了不可预测的结果。
使用调试器,我发现当我序列化时,每个属性都会调用解析器的CreateProperty方法。但是,如果我连续进行两次调用,则第二次调用时不会调用 CreateProperty 方法。我在 CreateProperty 中的断点在第二遍时从未被击中,而是在第一次被击中。
这是我的设置:
IContractResolver contractResolver = new ShouldSerializeContractResolver(fieldsToSerialize, this.Data);
var settings = new JsonSerializerSettings()
{
ContractResolver = contractResolver
};
_payload = JsonConvert.SerializeObject(this.Data, Formatting.None, settings);
Run Code Online (Sandbox Code Playgroud)
我的源值(this.Data)对于这两个调用是不同的。两次调用的结果 (_payload) 也不同。我认为没有任何内容被缓存。
我看到了由自定义 ContentNegotiator 引起的类似问题,但我不使用它。
为什么 CreateProperty 在第二遍时不会被命中?
我正在 Unity 中编写一个按类别提问和回答的游戏。这些类别是通过返回 JSON 文本的 PHP 脚本获取的,*当我在 UnityEditor 中使用此解决方案时,它可以正常工作,但是当我在移动设备上安装 .apk 时,反序列化不起作用*。
与 mysql 数据库的连接和 PHP 脚本工作正常,因为在我登录之前它工作正常
string json = [
{ "id_cat":"1",
"nombre_cat":"DAM",
"id_cat_padre":"0"
},
{ "id_cat":"4",
"nombre_cat":"ASIR",
"id_cat_padre":"0"
},
{ "id_cat":"5",
"nombre_cat":"DAW",
"id_cat_padre":"0"
}
]
Run Code Online (Sandbox Code Playgroud)
然后我将此字符串转换为类别列表
lsSubCategorias = JsonConvert.DeserializeObject<List<Categoria>>(json, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
Run Code Online (Sandbox Code Playgroud)
我已经在代码中添加了痕迹,它就在它停止的那一行上。
我使用 NuGet 安装了 Newtonsoft.Json,它出现在参考文献中。
我也只处理了一个类别对象而不是一个列表,但它也不起作用。这不是可视化问题,因为我已经创建了类别对象并用它们创建了按钮。
问题是它可以在 Unity 编辑器中运行,但不能在我的 Android 设备上运行
我的手机出现以下错误:
类型错误:System.PlatformNotSupportedException ToString 错误():Error.ToString()
如果我收到超出我控制范围的 JSON,它具有如下属性。
{"allow":"true"}
Run Code Online (Sandbox Code Playgroud)
我希望将其映射到bool
C# 中的属性。
我发现我可以使用属性对数字做类似的事情
[JsonNumberHandling(JsonNumberHandling.AllowReadingFromString)]
Run Code Online (Sandbox Code Playgroud)
但我怎样才能对布尔值做到这一点呢?
编辑:
我不确定为什么由于重复的问题而关闭此问题,因为另一个问题侧重于从整数转换为布尔值
我正在尝试使用 Jsonconverter 反序列化 json 字符串。我拥有 json 中的所有值,我还想提一下,特定的 json 是从 swagger 生成的,因此当我向 API 发送有效负载时,它会将其映射到对象模型中。
但是,当我尝试将其反序列化为同一个对象时,一切都是空的。我做错了什么?
这就是我反序列化它的方式
EmailUrlDto testEmailUrlDto = JsonConvert.DeserializeObject<EmailUrlDto>(jsonString);
Run Code Online (Sandbox Code Playgroud)
JSON格式
{
"templateUrl": "https://some.blob.url.here.com",
"paramsHtml": [
{
"name": "{{miniAdmin}}",
"value": "Heyth is Admin!"
},
{
"name": "{{cliBuTree}}",
"value": "I`m a treeee!"
}
],
"from": "string",
"subject": "string",
"message": "string",
"isHtmlBody": true,
"recipients": [
{
"recipient": "some.name@lol.com"
}
],
"cCRecipients": [
{
"recipient": "string"
}
],
"bCCRecipients": [
{
"recipient": "string"
}
]
}
Run Code Online (Sandbox Code Playgroud)
电子邮件网址
public class EmailUrlDto : EmailDto
{
[Required] …
Run Code Online (Sandbox Code Playgroud) 我冻结了模型(简化):
part 'initial_data_model.freezed.dart';
part 'initial_data_model.g.dart';
@freezed
class InitialDataModel with _$InitialDataModel {
const factory InitialDataModel() = Data;
const factory InitialDataModel.loading() = Loading;
const factory InitialDataModel.error([String? message]) = Error;
factory InitialDataModel.fromJson(Map<String, dynamic> json) => _$InitialDataModelFromJson(json);
}
Run Code Online (Sandbox Code Playgroud)
文档说明了如何在字段上分配自定义转换器,但不在模型本身上分配
我从后端和 api_provider 中的某个地方获取了 json 我确实
return InitialDataModel.fromJson(json);
无法控制 json 结构,没有“runtimeType”和其他愚蠢的冗余内容
当我想从 json 创建模型时,我打电话给fromJson
我
flutter: CheckedFromJsonException
Could not create `InitialDataModel`.
There is a problem with "runtimeType".
Invalid union type "null"!
Run Code Online (Sandbox Code Playgroud)
好吧,
我又有了api_provider
final apiProvider = Provider<_ApiProvider>((ref) => _ApiProvider(ref.read));
class _ApiProvider {
final Reader …
Run Code Online (Sandbox Code Playgroud) 我正在尝试保存压力图的流数据.基本上我有一个压力矩阵定义为:
double[,] pressureMatrix = new double[e.Data.GetLength(0), e.Data.GetLength(1)];
Run Code Online (Sandbox Code Playgroud)
基本上,我pressureMatrix
每10毫秒得到一个,我想将所有信息保存在JSON文件中,以便以后能够重现它.
我所做的是,首先,写下我称之为标题的标题,其中包含用于录制的所有设置,如下所示:
recordedData.softwareVersion = Assembly.GetExecutingAssembly().GetName().Version.Major.ToString() + "." + Assembly.GetExecutingAssembly().GetName().Version.Minor.ToString();
recordedData.calibrationConfiguration = calibrationConfiguration;
recordedData.representationConfiguration = representationSettings;
recordedData.pressureData = new List<PressureMap>();
var json = JsonConvert.SerializeObject(csvRecordedData, Formatting.None);
File.WriteAllText(this.filePath, json);
Run Code Online (Sandbox Code Playgroud)
然后,每次我得到一个新的压力图我创建一个新的线程来添加新的PressureMatrix
并重新写入该文件:
var newPressureMatrix = new PressureMap(datos, DateTime.Now);
recordedData.pressureData.Add(newPressureMatrix);
var json = JsonConvert.SerializeObject(recordedData, Formatting.None);
File.WriteAllText(this.filePath, json);
Run Code Online (Sandbox Code Playgroud)
大约20-30分钟后,我得到一个OutOfMemory异常,因为系统无法保存recordedData var,因为List<PressureMatrix>
它太大了.
如何处理这个以保存数据?我想保存24-48小时的信息.
我在将JSON字符串序列化为键/值字典时遇到问题。代码如下:
var parameters = "{\"id\":123}";
JsonConvert.DeserializeObject<Dictionary<string, string>>(parameters);
Run Code Online (Sandbox Code Playgroud)
这个例子看起来很完美,给了我{[id,123]}。
现在。当我将数字更改为“ 070809”时,突然出现异常:“ Newtonsoft.Json.JsonReaderException:输入字符串“ 070809”不是有效数字。路径“ id”,第1行,位置12。---> System.FormatException:其他不可解析的字符位于字符串的末尾。”
var parameters = "{\"id\":070809}";
JsonConvert.DeserializeObject<Dictionary<string, string>>(parameters);
Run Code Online (Sandbox Code Playgroud)
有什么建议么?
我正在尝试将字典保存到file
并将其内容加载到Dictionary对象。我正在使用以下代码将字典附加到文件中
string json = JsonConvert.SerializeObject(dict);
File.AppendAllText("config.fcj", json);
Run Code Online (Sandbox Code Playgroud)
但是在加载时,我一直收到以下错误。
Additional text encountered after finished reading JSON content: {. Path '', line 1, position 375.
Run Code Online (Sandbox Code Playgroud)
每次保存后是否需要添加新行?
我有两个相同类型的复杂对象。我想检查这两个对象之间是否有任何区别。我有两种选择来实现此目的,要么使用 JsonConvert.SerializeObject 将这两个对象转换为字符串,然后比较字符串,例如
var existingData = JsonConvert.SerializeObject(objOld);
var newData = JsonConvert.SerializeObject(objNew);
return existingData == newData;
Run Code Online (Sandbox Code Playgroud)
另一种选择是使用反射并循环遍历所有属性,如下所示。
protected bool MatchObject(object newObj, object oldObj)
{
Type currentType = newObj.GetType();
PropertyInfo[] props = currentType.GetProperties();
bool isSameObject = true;
foreach (var prop in props)
{
var i = prop.GetValue(newObj);
var f = prop.GetValue(oldObj);
if (!object.Equals(i, f))
{
isSameObject = false;
break;
}
}
return isSameObject;
}
Run Code Online (Sandbox Code Playgroud)
从性能角度来看,上述哪种方法更有效?
我在 .net 6.0 上有 c# webapi。我正在将 postdata 从客户端获取到 param_class 到 _input 中。该类如下:
public class param_class
{
public string? name { get; set; }
public object? value { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
客户端以 json 字符串形式发送数据,如下所示:
{
"name": "lotnum",
"value": 143
}
Run Code Online (Sandbox Code Playgroud)
在 webapi 中,当我获取值字段时,会抛出错误 JsonElement 无法转换为 Int。
var v = (int)_input.value;
Run Code Online (Sandbox Code Playgroud)
如何在.net 6.0的webapi中将JsonElement转换为其他类型?
jsonconvert ×12
c# ×10
json ×6
json.net ×5
.net ×4
dictionary ×2
.net-6.0 ×1
.net-core ×1
asp.net-core ×1
flutter ×1
freezed ×1
iconvertible ×1
webapi ×1