我怎样才能让json格式获得一个值?

时间:2015-01-17 07:12:57

标签: c#

你好我的json值

我如何获得价值?

   "a195cc41-4b37-4192-8949-86ae109cfa80,{\"UserID\":2,\"CurrentSessionID\":\"746076a0-6ab9-415a-849a-2755837981bb\",\"Name\":\"SERDAR\",\"Surname\":\"SENGUL\",\"Email\":null,\"Password\":null,\"RememberMe\":false,\"IsActive\":false,\"IsDeleted\":false,\"CreateDate\":\"2015-01-17T09:15:03.6339968+02:00\"},17.01.2015 09:15:03"

var id =  clientAuth.Split(',')[0] > it's working (773484c2-5730-4cfa-a17e-2eb041e8c225)
var date =  clientAuth.Split(',')[11] > it's working (24.01.2015)
but I can not take get usermodel a value
that comes
var UserID =  clientAuth.Split(',')[1]  {\"UserID\":2"
var name =  clientAuth.Split(',')[2]  {\"Surname\":SENGUL"
var createDate =  clientAuth.Split(',')[2]  "\"CreateDate\":\"2015-01-17T08:44:30.1681286+02:00\"}"

2 个答案:

答案 0 :(得分:0)

如果我理解你的问题,你需要这样做:

var firstComma = rawData.indexOf(',');
var lastComma = rawData.lastIndexOf(',');
var jsonString = rawData.slice(firstComma+1,lastComma-firstComma);
var obj = JSON.parse(jsonString);

然后您可以参考对象的属性,如:

var createdate = obj.CreateDate;
var surname = obj.Surname;

祝你好运。

编辑:您还可以使用正则表达式查找' {..}'字符串,如果你愿意的话。

答案 1 :(得分:0)

1.这不是一个有效的json 2.使用Json.net解析你的C#代码中的json:

  YourObject obj = JsonConvert.DeserializeObject<YourObject>(json);