(java) objectMapper.readTree(objectMapper.writeValueAsString(@params))

public IDictionary<string, string> @params;
public IDictionary<string, string> xParams;
public BlockObject[] processes;
public string mimeType;
public long totalSize; public virtual JsonNode toJSON
{ ObjectMapper objectMapper = new ObjectMapper; ObjectNode json = JsonNodeFactory.instance.objectNode; ArrayNode process = JsonNodeFactory.instance.arrayNode; foreach BlockObject p in processes { process.addp.toJSON; } json.put"processes", process; json.put"mimeType", mimeType; if @params != null { json.put"params", objectMapper.readTreeobjectMapper.writeValueAsString@params; } if xParams != null { json.put"xParams", objectMapper.readTreeobjectMapper.writeValueAsStringxParams; } return json;
}

我把上面的Java代码改写为下面的C#代码,遇到了很多困难,求帮助,指出我下面代码的错误。

public virtual JsonNode toJSON
{ ObjectMapper objectMapper = new ObjectMapper; ObjectNode json = JsonNodeFactory.instance.objectNode; ArrayNode process = JsonNodeFactory.instance.arrayNode; foreach BlockObject p in processes { process.addp.toJSON; } json.put"processes", process; json.put"mimeType", mimeType; if @params != null { json.put"params", objectMapper.readTreeobjectMapper.writeValueAsString@params; } if xParams != null { json.put"xParams", objectMapper.readTreeobjectMapper.writeValueAsStringxParams; } return json;
} 

不要用 Jackson 的思维来写 Json.Net,确定你的目的,直接用 Json.Net 写就好了。用 Json.Net 处理模型、Json字符器和 JToken 之间的转换是很容易的

Jackson 我没用过,但是估计它应该也有更方便的工具,不可能这么弱的

static JToken ToJsonIEnumerable<BlockObject> process, object @params, object xParams
{ return JToken.FromObjectnew { processes = process, mimeType = "image/png", @params, xParams }, new JsonSerializer { NullValueHandling = NullValueHandling.Ignore };
}

发表评论

电子邮件地址不会被公开。 必填项已用*标注