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.add(p.toJSON());
}
json.put("processes", process);
json.put("mimeType", mimeType);
if (@params != null)
{
json.put("params", objectMapper.readTree(objectMapper.writeValueAsString(@params)));
}
if (xParams != null)
{
json.put("xParams", objectMapper.readTree(objectMapper.writeValueAsString(xParams)));
}
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.add(p.toJSON());
}
json.put("processes", process);
json.put("mimeType", mimeType);
if (@params != null)
{
json.put("params", objectMapper.readTree(objectMapper.writeValueAsString(@params)));
}
if (xParams != null)
{
json.put("xParams", objectMapper.readTree(objectMapper.writeValueAsString(xParams)));
}
return json;
}
java c# jackson newtonsoft.json
爱新觉罗.能干
9 years, 9 months ago
Answers
不要用 Jackson 的思维来写 Json.Net,确定你的目的,直接用 Json.Net 写就好了。用 Json.Net 处理模型、Json字符器和 JToken 之间的转换是很容易的
Jackson 我没用过,但是估计它应该也有更方便的工具,不可能这么弱的
static JToken ToJson(IEnumerable<BlockObject> process, object @params, object xParams)
{
return JToken.FromObject(new {
processes = process,
mimeType = "image/png",
@params,
xParams
}, new JsonSerializer {
NullValueHandling = NullValueHandling.Ignore
});
}
亚当奎特斯
answered 9 years, 9 months ago