public class UserAction {
private final UUID uuid;
private String userId;
/* more fields, setters and getters here */
public UserAction(){
this.uuid = UUID.fromString(new com.eaio.uuid.UUID().toString());
}
public UserAction(UUID uuid){
this.uuid = uuid;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final UserAction other = (UserAction) obj;
if (this.uuid != other.uuid && (this.uuid == null || !this.uuid.equals(other.uuid))) {
return false;
}
return true;
}
@Override
public …Run Code Online (Sandbox Code Playgroud) 我有来自服务器的下一个响应 -
{"response":[{"uid":174952xxxx,"first_name":"xxxx","last_name":"xxx"}]}
Run Code Online (Sandbox Code Playgroud)
我试图以下一种方式反序化 -
JsonConvert.DeserializeObject<T>(json);
Run Code Online (Sandbox Code Playgroud)
其中T = VkUser的列表,但是我收到了错误.
[JsonObject]
public class VkUser
{
[JsonProperty("uid")]
public string UserId { get; set; }
[JsonProperty("first_name")]
public string FirstName { get; set; }
[JsonProperty("last_name")]
public string LastName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我一直在尝试
public class SomeDto // maybe Response as class name will fix it but I don't want such name
{
public List<VkUser> Users {get;set;}
}
Run Code Online (Sandbox Code Playgroud)
什么反序列化选项可以帮助我?
我已经阅读了另外两篇有关反序列化错误的帖子,但我没有得到任何结论,所以我发布了自己的问题.
我有一个返回JSON的WCF服务.在对特定类型进行反序列化时,它会失败.
为了让您轻松复制错误,我将下面的JSON硬编码与调用一起.
基本上你应该能够复制/粘贴下面的代码并看到它失败.
消费者需要将其反序列化为工作对象.s.反序列化失败,并显示标题中指出的错误消息.
注意:我意识到我的JSON中有斜杠.那些是为了方便逃避报价.谢谢.
示例代码:
var s = new JavaScriptSerializer();
var jstr =
"[{\"UserId\":1,\"WorkoutId\":1,\"WorkoutInfo\":[\"Step 1\",\"Step 2\"]},{\"UserId\":2,\"WorkoutId\":2,\"WorkoutInfo\":[\"Step 1a\",\"Step 2a\"]},{\"UserId\":5,\"WorkoutId\":0,\"WorkoutInfo\":[\"new work1\",\"new work 1\",\"new work 1\"]}]";
Workout blah = s.Deserialize<Workout>(jstr);
var response = ServicePOST<Workout>("AddUserWorkout", workout);
Run Code Online (Sandbox Code Playgroud)
和锻炼课程:
public class Workout
{
public int UserId { get; set; }
public List<string> WorkoutInfo { get; set; }
}
Run Code Online (Sandbox Code Playgroud) 如果我有这样的类结构:
public abstract class Parent {
private Long id;
...
}
public class SubClassA extends Parent {
private String stringA;
private Integer intA;
...
}
public class SubClassB extends Parent {
private String stringB;
private Integer intB;
...
}
Run Code Online (Sandbox Code Playgroud)
是否有另一种方法来反序列化不同的@JsonTypeInfo呢?在我的父类上使用此批注:
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "objectType")
Run Code Online (Sandbox Code Playgroud)
我宁愿不必强制我的API的客户端包括"objectType": "SubClassA"反序列化子Parent类.
@JsonTypeInfo杰克逊是否提供了一种注释子类并通过唯一属性将其与其他子类区分开来的方式,而不是使用?在上面的示例中,这将是"如果JSON对象"stringA": ...将其反序列化为SubClassA,如果它已将其"stringB": ...反序列化为SubClassB".
Golang将结构序列化和反序列化为字符串的最佳方式(完整性和性能)是什么,反之亦然?
例如,如果我有这个结构:
struct Session {
Properties map[string]interface{}
Permissions []int64
}
Run Code Online (Sandbox Code Playgroud)
我想将它存储在Redis上并将其取回.我试过保存,int和字符串,没关系,但是如何存储struct对象?
conn := redisConnectors.Get()
// set example
_, err := conn.Do(`SETEX`, `uid_key`, EXPIRE_SEC, user_id)
_, err = conn.Do(`SETEX`, `email_key`, EXPIRE_SEC, login_email)
// get example
user_id, err := redis.Int64(conn.Do(`GET`, `uid_key`))
login_email, err := redis.String(conn.Do(`GET`, `email_key`))
Run Code Online (Sandbox Code Playgroud) 在jackson中使用for的@JsonTypeInfo和@JsonSubTypes注释是什么?
public class Lion extends Animal {
private String name;
@JsonCreator
public Lion(@JsonProperty("name") String name) {
this.name = name;
}
public String getName() {
return name;
}
public String getSound() {
return "Roar";
}
public String getType() {
return "carnivorous";
}
public boolean isEndangered() {
return true;
}
@Override
public String toString() {
return "Lion [name=" + name + ", getName()=" + getName() + ", getSound()=" + getSound() + ", getType()=" + getType() + ", isEndangered()="
+ isEndangered() + "]"; …Run Code Online (Sandbox Code Playgroud) 客户端接收正式的JSON内容"{\"Id\":[1,2,3],\"Size\":56}",但在反序列化字节数组时出错.
1以下语句中出现错误
IRestResponse<key> response = client.Execute<key>(request);
Run Code Online (Sandbox Code Playgroud)
2错误消息是"没有为此对象定义无参数构造函数".
3客户端大小的对象类与服务器端的对象类相同:
public class key
{
public byte[] id { get; set; }
public int Size { set; get; }
}
Run Code Online (Sandbox Code Playgroud)
4我尝试通过JSON格式传递包含字符串和整数的对象,这一切都很好但是字节数组.
可能重复:将
JSON反序列化为多个C#子类之一
我有遵循JSON模式的只读访问:
{ items: [{ type: "cat", catName: "tom" }, { type: "dog", dogName: "fluffy" }] }
Run Code Online (Sandbox Code Playgroud)
我想将其中的每一个反序列化为各自的类型:
class Cat : Animal {
string Name { get; set; }
}
class Dog : Animal {
string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我唯一想到的是将它们反序列化为一个dynamic对象,或者Dictionary<string, object>然后从那里构造这些对象.
我可能会遗漏一些JSON框架中的内容....
你的方法是什么?=]
我正在使用NewtonSoft JObject解析JSON字符串.如何以编程方式从动态对象获取值?我想简化代码,不要为每个对象重复自己.
public ExampleObject GetExampleObject(string jsonString)
{
ExampleObject returnObject = new ExampleObject();
dynamic dynamicResult = JObject.Parse(jsonString);
if (!ReferenceEquals(dynamicResult.album, null))
{
//code block to extract to another method if possible
returnObject.Id = dynamicResult.album.id;
returnObject.Name = dynamicResult.album.name;
returnObject.Description = dynamicResult.albumsdescription;
//etc..
}
else if(!ReferenceEquals(dynamicResult.photo, null))
{
//duplicated here
returnObject.Id = dynamicResult.photo.id;
returnObject.Name = dynamicResult.photo.name;
returnObject.Description = dynamicResult.photo.description;
//etc..
}
else if..
//etc..
return returnObject;
}
Run Code Online (Sandbox Code Playgroud)
有什么办法可以将"if"语句中的代码块提取到一个单独的方法,例如:
private void ExampleObject GetExampleObject([string of desired type goes here? album/photo/etc])
{
ExampleObject returnObject = new ExampleObject(); …Run Code Online (Sandbox Code Playgroud) 发送的JSON:
{
"Banner": "ABC"
}
Run Code Online (Sandbox Code Playgroud)
Java模型:
...
@JsonIgnoreProperties(ignoreUnknown = true)
@JsonSerialize(include = JsonSerialize.Inclusion.NON_NULL)
public class BannerData implements java.io.Serializable {
private static final long serialVersionUID = 5664846645733319592L;
@JsonProperty(value = "Banner")
private String banner;
public String getBanner() {
return banner;
}
public void setBanner(String banner) {
this.banner = banner;
}
}
Run Code Online (Sandbox Code Playgroud)
控制器:
@RequestMapping(value = {"/GetBanner"}, method = RequestMethod.POST)
@ResponseBody
public ResponseEntity<String> enrollCustomer(@RequestBody BannerData body, HttpServletRequest request) throws Exception {
...
}
Run Code Online (Sandbox Code Playgroud)
请求/GetBanner返回:
客户端发送的请求在语法上是不正确的.
工程确定当JSON变更为(小写字母命名的就是Java字段名):
{
"banner": "ABC"
}
Run Code Online (Sandbox Code Playgroud)
但是我需要在 …