我正在尝试解码Firebase DataSnapshot中的数据,以便可以使用JSONDecoder对其进行解码.
当我使用URL通过网络请求访问它(获取Data对象)时,我可以正确解码此数据.
不过,我想用火力地堡API直接获取数据,使用observeSingleEvent上描述这个页面.
但是,当我这样做时,我似乎无法将结果转换为Data对象,我需要使用JSONDecoder.
是否可以使用DataSnapshot进行新的JSON解码?这怎么可能?我似乎无法弄明白.
我以JSON格式启动此RestSharp查询:
var response = restClient.Execute<Report>(request);
Run Code Online (Sandbox Code Playgroud)
我得到的回复包含这些数据
[
{
"Columns":
[
{"Name":"CameraGuid","Type":"Guid"},
{"Name":"ArchiveSourceGuid","Type":"Guid"},
{"Name":"StartTime","Type":"DateTime"},
{"Name":"EndTime","Type":"DateTime"},
{"Name":"TimeZone","Type":"String"},
{"Name":"Capabilities","Type":"UInt32"}
],
"Rows":
[
[
"00000001-0000-babe-0000-00408c71be50",
"3782fe37-6748-4d36-b258-49ed6a79cd6d",
"2013-11-27T17:52:00Z",
"2013-11-27T18:20:55.063Z",
"Eastern Standard Time",
2147483647
]
]
}
]
Run Code Online (Sandbox Code Playgroud)
我正在尝试将其反序列化为这组类:
public class Report
{
public List<ReportResult> Results { get; set; }
}
public class ReportResult
{
public List<ColumnField> Columns { get; set; }
public List<RowResult> Rows { get; set; }
}
public class ColumnField
{
public string Name { get; set; }
public string Type { get; …Run Code Online (Sandbox Code Playgroud) 我有一个JSON字符串,看起来像:
"{\"Id\":\"fb1d17c7298c448cb7b91ab7041e9ff6\",\"Name\":\"John\",\"DateOfBirth\":\"\\/Date(317433600000-0000)\\/\"}"
Run Code Online (Sandbox Code Playgroud)
我正在尝试将其反序列化为object(我正在实现一个缓存接口)
我遇到的麻烦就是我用的时候
JsonSerializer.DeserializeFromString<object>(jsonString);
Run Code Online (Sandbox Code Playgroud)
它回来了
"{ID:6ed7a388b1ac4b528f565f4edf09ba2a,名称:约翰,出生日期:/日期(317433600000-0000)/}"
是对的吗?
我无法断言任何事情......我也不能使用动态关键字....
有没有办法从ServiceStack.Text库返回一个匿名对象?
c# anonymous-types servicestack json-deserialization servicestack-text
我有一个JsonObject例如
JsonObject jsonObject = {"keyInt":2,"keyString":"val1","id":"0123456"}
Run Code Online (Sandbox Code Playgroud)
每个JSONObject都包含一个"id"条目,但是没有确定其他键/值对,所以我想创建一个具有2个属性的对象:
class myGenericObject {
Map<String, Object> attributes;
String id;
}
Run Code Online (Sandbox Code Playgroud)
所以我希望我的属性映射看起来像这样:
"keyInt" -> 4711
"keyStr" -> "val1"
Run Code Online (Sandbox Code Playgroud)
我找到了这个解决方案
Map<String, Object> attributes = new HashMap<String, Object>();
Set<Entry<String, JsonElement>> entrySet = jsonObject.entrySet();
for(Map.Entry<String,JsonElement> entry : entrySet){
attributes.put(entry.getKey(), jsonObject.get(entry.getKey()));
}
Run Code Online (Sandbox Code Playgroud)
但是值被""括起来
"keyInt" -> "4711"
"keyStr" -> ""val1""
Run Code Online (Sandbox Code Playgroud)
如何获得普通值(4711和"val1")?
输入数据:
{
"id": 0815,
"a": "a string",
"b": 123.4,
"c": {
"a": 1,
"b": true,
"c": ["a", "b", "c"]
}
}
Run Code Online (Sandbox Code Playgroud)
要么
{
"id": 4711,
"x": false,
"y": "y?",
}
Run Code Online (Sandbox Code Playgroud) 我目前正在使用Gson在Java中编写RSS提要解析器.我正在将RSS'XML转换为JSON,然后使用Gson将JSON反序列化为Java POJO(有点迂回,但有一个原因).对于下面列出的Feed#1(BBC)进行反序列化,一切都运行正常,但对于下面列出的Feed#2(NPR),我开始抛出异常.
我想我已经确定了问题,但我不确定如何解决它:
问题出现在这两个RSS源(例如):
对于这些不同的RSS源,称为"guid"的字段作为a)具有2个字段的对象(如在BBC RSS Feed中)或b)字符串(如在NPR RSS Feed中)返回.
以下是相关JSON的一些释义版本:
BBC RSS Feed
// is returning 'guid' as an object
"item" :
[
{
// omitted other fields for brevity
"guid" : {
"isPermalink" : false,
"content" : "http:\/\/www.bbc.co.uk\/news\/uk-england-33745057"
},
},
{
// ...
}
]
Run Code Online (Sandbox Code Playgroud)
NPR RSS Feed
// is returning 'guid' as a string
"item" :
[
{
// omitted other fields for brevity
"guid" : …Run Code Online (Sandbox Code Playgroud) 我需要帮助了解如何使用JsonConverter.ReadJson方法将任意数量的类型(字符串,布尔值,日期,整数,数组,对象)的值转换为特定的自定义类型.
例如,我有以下内容;
public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
{
//where reader.Value could be a string, boolean, Date, int, array, object
//and in this example the value of reader.Value is a string
return new MyCustomType(reader.Value);
}
Run Code Online (Sandbox Code Playgroud)
但这会给出错误;
Compilation error (line 115, col 36): Argument 1: cannot convert from 'object' to 'string'
Run Code Online (Sandbox Code Playgroud)
我对C#有点绿,只需要帮助完成这项工作.
我正在使用RestTemplate来使用REST Api.我从API获得的响应有很多嵌套对象.这里有一个小片段作为例子:
"formularios": [
{
"form_data_id": "123006",
"form_data": {
"form_data_id": "123006",
"form_id": "111",
"efs": {
"1": {},
"2": "{\"t\":\"c\",\"st\":\"m\",\"v\":[{\"id\":\"3675\",\"l\":\"a) Just an example\",\"v\":\"1\"},{\"id\":\"3676\",\"l\":\"b) Another example.\",\"v\":\"2\"}]}"
}
}
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是大多数时候"1"实际上有内容,就像"2"一样,而jackson只是将它解析为对象"efs"上的String.但有时候,就像在代码片段中一样,API将其发送为空,并且jackson将其作为对象,这给了我一个错误,说明了一些关于START_OBJECT的内容(不记得确切的错误,但对于这个问题并不重要) ).
所以我决定创建一个自定义反序列化器,所以当jackson读取"1"时,它会忽略空对象并将其解析为空字符串.
这是我的自定义反序列化器:
public class CustomDeserializer extends StdDeserializer<Efs> {
public CustomDeserializer(Class<Efs> t) {
super(t);
}
@Override
public Efs deserialize(JsonParser jp, DeserializationContext dc)
throws IOException, JsonProcessingException {
String string1 = null;
String string2 = null;
JsonToken currentToken = null;
while ((currentToken = jp.nextValue()) != null) {
if (currentToken.equals(JsonToken.VALUE_STRING)) {
if (jp.getCurrentName().equals("1")) {
string1 = …Run Code Online (Sandbox Code Playgroud) 我有两个服务应该通过Kafka. 让我们调用第一个服务WriteService和第二个服务QueryService。
在WriteService端,我对生产者有以下配置。
@Configuration
public class KafkaProducerConfiguration {
@Value("${spring.kafka.bootstrap-servers}")
private String bootstrapServers;
@Bean
public Map<String, Object> producerConfigs() {
Map<String, Object> props = new HashMap<>();
// list of host:port pairs used for establishing the initial connections to the Kakfa cluster
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG,
bootstrapServers);
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG,
StringSerializer.class);
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG,
JsonSerializer.class);
return props;
}
@Bean
public ProducerFactory<String, Object> producerFactory() {
return new DefaultKafkaProducerFactory<>(producerConfigs());
}
@Bean
public KafkaTemplate<String, Object> kafkaTemplate() {
return new KafkaTemplate<>(producerFactory());
}
}
Run Code Online (Sandbox Code Playgroud)
我正在尝试发送类的对象 com.example.project.web.routes.dto.RouteDto
在 …
spring apache-kafka json-deserialization spring-boot spring-kafka
我正在尝试将 json 反序列化为具有 type 属性的对象Dictionary<string,string>。我将字典的比较器指定为StringComparer.OrdinalIgnoreCase。这是这个类:
class DictionaryTest
{
public Dictionary<string, string> Fields { get; set; }
public DictionaryTest()
{
Fields = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
}
}
Run Code Online (Sandbox Code Playgroud)
但是当反序列化发生时,比较器将更改为通用比较器。因此,我无法以不区分大小写的方式访问字典的键。
var points = new Dictionary<string, string>
{
{ "James", "9001" },
{ "Jo", "3474" },
{ "Jess", "11926" }
};
var testObj = new DictionaryTest{Fields = points};
var dictionaryJsonText = JsonSerializer.Deserialize<DictionaryTest>(JsonSerializer.Serialize(testObj, options:new JsonSerializerOptions()
{
IgnoreNullValues = true,
WriteIndented = false,
PropertyNamingPolicy = null,
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
DictionaryKeyPolicy = JsonNamingPolicy.CamelCase, …Run Code Online (Sandbox Code Playgroud) 我有以下HAL + JSON示例:
{
"id": "4a17d6fe-a617-4cf8-a850-0fb6bc8576fd",
"country": "DE",
"_embedded": {
"company": {
"name": "Apple",
"industrySector": "IT",
"owner": "Klaus Kleber",
"_embedded": {
"emailAddresses": [
{
"id": "4a17d6fe-a617-4cf8-a850-0fb6bc8576fd",
"value": "test2@consoto.com",
"type": "Business",
"_links": {
"self": {
"href": "https://any-host.com/api/v1/customers/1234"
}
}
}
],
"phoneNumbers": [
{
"id": "4a17d6fe-a617-4cf8-a850-0fb6bc8576fd",
"value": "01670000000",
"type": "Business",
"_links": {
"self": {
"href": "https://any-host.com/api/v1/customers/1234"
}
}
}
],
},
"_links": {
"self": {
"href": "https://any-host.com/api/v1/customers/1234"
},
"phoneNumbers": {
"href": "https://any-host.com/api/v1/customers/1234"
},
"addresses": {
"href": "https://any-host.com/api/v1/customers/1234"
},
} …Run Code Online (Sandbox Code Playgroud) c# ×5
json ×4
java ×3
gson ×2
json.net ×2
spring ×2
.net-5 ×1
apache-kafka ×1
jackson ×1
restsharp ×1
resttemplate ×1
servicestack ×1
spring-boot ×1
spring-kafka ×1
swift ×1