[英]How do I deserialize a json list string to a list of object strings?
I have a string object which I am trying to deserilize but when there is a json object in an another json object it has issues deserilizing.我有一个字符串对象,我试图对其进行反序列化,但是当另一个 json 对象中有一个 json 对象时,它在反序列化时出现问题。
[
{
"attributes": {
"type": "Account",
"url": "/services/data/77.0/object/Account/1234"
},
"Id": "1234",
"IsDeleted": false,
"MasterRecordId": null,
"Name": "Stevens Smith",
"LastName": null,
"FirstName": null,
"Salutation": null,
},
{
"attributes": {
"type": "Account",
"url": "/services/data/v77.0/object/Account/12345"
},
"Id": "12345",
"IsDeleted": false,
"MasterRecordId": null,
"Name": "Mr John Smith",
"LastName": null,
"FirstName": null,
"Salutation": null,
}
]
My Class is defined as follows:我的班级定义如下:
public Account() {
public string? attributes;
public string? id;
public string? isDeleted;
public string? masterRecordId;
public string? name;
public string? lastName;
public string? firstName;
public string? salutation;
}
The error message I get is Unexpected character encountered while parsing value: {.我收到的错误消息是解析值时遇到意外字符:{。 Path '[0].attributes', line 3, position 19.路径“[0].attributes”,第 3 行,位置 19。
You should have two classes你应该有两个类
public class Account() {
public Attrubute attributes;
public string? id;
public string? isDeleted;
public string? masterRecordId;
public string? name;
public string? lastName;
public string? firstName;
public string? salutation;
}
public class Attrubute() {
public string? type;
public string? url;
}
If you just want to save the Attrubute
object as a string you could do something like this (syntax could be different depending if you're using newtonsoft or not)如果您只想将Attrubute
对象保存为字符串,您可以这样做(语法可能会有所不同,具体取决于您是否使用 newtonsoft)
public class Account() {
public Attrubute attributes;
public string? id;
public string? isDeleted;
public string? masterRecordId;
public string? name;
public string? lastName;
public string? firstName;
public string? salutation;
public string attributesString => attributes != null ? JsonConvert.Serialize(attributes) : string.empty;
}
You want don't want to define the Attrubute
class.你不想定义Attrubute
类。 You could make it dynamic object and if you want the string version - you could convert within your class.你可以使它成为动态对象,如果你想要字符串版本 - 你可以在你的类中进行转换。 So something like this所以像这样
public class Account {
public string? id;
public string? isDeleted;
public string? masterRecordId;
public string? name;
public string? lastName;
public string? firstName;
public string? salutation;
public dynamic attributes;
public string Attributes => attributes != null ? JsonConvert.SerializeObject(attributes) : string.Empty;
}
A lot of your variables are the wrong type.你的很多变量都是错误的类型。 It's much safer to use the correct types - that way if you ever reserialize to JSON it will stay valid (for example isDeleted - even if you parse it in as a string, reserializing would store it as:"false" instead of:false).使用正确的类型要安全得多——这样,如果你重新序列化为 JSON,它将保持有效(例如 isDeleted——即使你将它解析为字符串,重新序列化会将其存储为:“false”而不是:false) .
public class Account
{
public Attrubute attributes;
public string stringAttributes //if you need a string accessor to attributes you can just use a property
{
get {
return $"{attributes?.type}:{attributes?.url}";
}
}
public string? id;
public bool isDeleted;
public string? masterRecordId;
public string? name;
public string? lastName;
public string? firstName;
public string? salutation;
}
public class Attrubute
{
public string? type;
public string? url;
}
If you do want to keep things as string, a better option is to leave it as json instead with JObject, and call.ToString() to return the json value when needed如果您确实想将其保留为字符串,更好的选择是将其保留为 json 而不是 JObject,并在需要时调用.ToString() 返回 json 值
public class Account
{
public JObject attributes;
public string? id;
public JObject isDeleted;
public string? masterRecordId;
public string? name;
public string? lastName;
public string? firstName;
public string? salutation;
public string GetAttributesJson() {
return attributes.ToString();
}
}
if you need attributes as string you can do it using a counstructor for example, but before you will have to fix the class by adding getter/setter如果你需要属性作为字符串,你可以使用一个 counstructor 来实现,但是在你必须通过添加 getter/setter 来修复类之前
List<Account> accounts = JsonConvert.DeserializeObject<List<Account>>(json);
public class Account
{
public int? id { get; set; }
public string? attributes { get; set; }
//.... another properties
[JsonConstructor]
public Account(JObject attributes)
{
this.attributes = string.Join(",", attributes.Properties());
//or a json string
this.attributes = attributes.ToString();
// or a url
this.attributes = (string)attributes["url"]
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.