json字符串反序列化

wanbolantian 2016-07-22 10:17:50
这是调用某宝接口返回的json字符串:
{"tmall_msf_identify_status_query_response":{"result":"{errorMessage=服务类型不对, gmtCurrentTime=1469153603575, errorCode=352, class=com.tmall.msf.common.dto.Result, object=null, success=false, costTime=null}","request_id":"3jvhmoxlvkco"}}

我试图用newtonjson的DeserializeObject方法,将其反序列化成.net对像,为此我建立了两个类
 public  class result
{
public string errorMessage { get; set; }
public long gmtCurrentTime { get; set; }
public int errorCode { get; set; }
public string @class { get; set; }
public object @object { get; set; }
public bool success { get; set; }
public object costTime { get; set; }
}

public class tmall_msf_identify_status_query_response
{
public result result { get; set; }
public string request_id { get; set; }
}

但发现反序列化后得到的obj总是null值
 tmall_msf_identify_status_query_response obj = JsonConvert.DeserializeObject<tmall_msf_identify_status_query_response>(json);

我该如何反序列化才能得到正确的结果?
...全文
552 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wanbolantian 2016-07-22
  • 打赏
  • 举报
回复
引用 3 楼 yuankaiwsl 的回复:
应该再把result的值再一次反序列化
result里面的值,不是标准的json字符,在反序列化的时候,会报错 Invalid JavaScript property identifier character: =. Path '', line 1, position 13.
巴士上的邂逅 2016-07-22
  • 打赏
  • 举报
回复
应该再把result的值再一次反序列化
巴士上的邂逅 2016-07-22
  • 打赏
  • 举报
回复
通过VS2013功能“将Json粘贴为类”结果是这样的
public class Rootobject
{
    public Tmall_Msf_Identify_Status_Query_Response tmall_msf_identify_status_query_response { get; set; }
}

public class Tmall_Msf_Identify_Status_Query_Response
{
    public string result { get; set; }
    public string request_id { get; set; }
}
你会发现result后边的是在双引号里面的,引号里面的都是result的值
  • 打赏
  • 举报
回复
对方返回的result是json序列化后的字符串,你要定义result为string,第一次反序列化后,再对result字符串进行反序列化
wanbolantian 2016-07-22
  • 打赏
  • 举报
回复
6楼方法可用,顺便吐槽某宝,为毛不返回标准的json
xuzuning 2016-07-22
  • 打赏
  • 举报
回复
                    var t = Regex.Replace(x.Value.ToString(), "=([^,}]+)", ":'$1'"); 
改成这样要好些
                    var t = Regex.Replace(x.Value.ToString(), "=(null|false|true)", ":$1");
                    t = Regex.Replace(t, "=([^,}]+)", ":'$1'");
xuzuning 2016-07-22
  • 打赏
  • 举报
回复
            var s = File.ReadAllText("d5.txt", Encoding.Default);
var a = JObject.Parse(s);
foreach (var x in (JObject)a["tmall_msf_identify_status_query_response"])
{
Console.WriteLine("{0} : {1}", x.Key, x.Value);
if (x.Key == "result")
{
//这样转成 json 串
var t = Regex.Replace(x.Value.ToString(), "=([^,}]+)", ":'$1'");

foreach (var y in JObject.Parse(t))
{
Console.WriteLine("{0} : {1}", y.Key, y.Value);
}
}
}
巴士上的邂逅 2016-07-22
  • 打赏
  • 举报
回复
如果格式固定,可以通过处理字符串,存入一个Dictionary

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧