Var返回的值为{"addnewuserresult":40}.我如何从中得到40并使用此值. [英] Var returned value as {"addnewuserresult":40}. How can I get 40 from this and use this value..

查看:75
本文介绍了Var返回的值为{"addnewuserresult":40}.我如何从中得到40并使用此值.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个具有json格式的3层体系结构应用程序.


I have developing a 3 layer architecture application with json format.


var serialize = new System.Web.Script.Serialization.JavaScriptSerializer();
                  var test = serviceCall.serviCallPostMethod("AdminService", "AddNewUser", serialize.Serialize(oUser));



Adminservice是我通过BAL&使用的服务的服务名称. DAL层.

现在我收到类似
的值 var test = {"AddNewUserResult":40}


如何从这个值中使用40 ...

我尝试过的事情:

var serialize = new System.Web.Script.Serialization.JavaScriptSerializer();
var test = serviceCall.serviCallPostMethod("AdminService","AddNewUser",serialize.Serialize(oUser));



Adminservice is my service name through the service I use BAL & DAL layer.

Now I receive a value like
var test= {"AddNewUserResult":40}


How can I use 40 from this value...

What I have tried:

var serialize = new System.Web.Script.Serialization.JavaScriptSerializer();
var test = serviceCall.serviCallPostMethod("AdminService", "AddNewUser", serialize.Serialize(oUser));

推荐答案

所获得的称为JSON [ ^ ] ...在.NET中处理此类数据的最佳库之一是: Json.NET-Newtonsoft [ ^ ].使用它!
What you got, called JSON[^]... One of the best libraries to handle such data in .NET is here: Json.NET - Newtonsoft[^]. Use it!


好吧,正确的方法是沿着
行声明一个类.
公共类RESTAddNewUserResult
{
public int AddNewUserResult {获取;设置;}
}

然后

//假设字符串test = serviceCall ... Result

RESTAddNewUserResult RESTResult = JsonConvert.DeserialzeObject< RESTAddNewUserResult>(测试);

(我的名字很糟糕,但是我希望您能明白..然后有了RESTResult.AddNewUserResult)
well, the correct way would be to declare a class along the line of

public class RESTAddNewUserResult
{
public int AddNewUserResult {get; set;}
}

and then

// Assuming String test = serviceCall ... Result

RESTAddNewUserResult RESTResult = JsonConvert.DeserialzeObject<RESTAddNewUserResult>(test);

(My naming is terrible, but I hope you get the idea .. you then have RESTResult.AddNewUserResult)


与流行的看法相反:)没有必要引用第三方库完成如此琐碎的任务.

您可以通过两种方式反序列化此数据;

Contrary to popular belief :) it''s not necessary to reference third party libraries for such a trivial task.

Two ways you can deserialise this data;

var serialize = new System.Web.Script.Serialization.JavaScriptSerializer();
var test = "{\"AddNewUserResult\":40}";

// Method 1 - use a dictionary
IDictionary<string, object> myResult = (IDictionary<string, object>) serialize.DeserializeObject(test);
            
int r = (int)myResult["AddNewUserResult"];


// Method 2 = use a custom class
MyData d = serialize.Deserialize<MyData>(test);

r = d.AddNewUserResult;



自定义类



The custom class

public class MyData
{
    public int AddNewUserResult { get; set; }
}



字典方法适用于像您这样的简单json数据,但是如果数据开始变得更加复杂,则可能需要使用自定义类方法.

附带说明一下,我不建议像这样在内部传递json,它太脆弱了并且容易出错,因为它类型安全.如果您正在调用的是REST服务,则可以使用restsharp之类的东西,但是我建议您切换到WCF服务.



The dictionary method is fine for simple json data like you have, but if the data starts to get more complicated you may need to move to the custom class method.

As a side note, I wouldn''t recommend passing json around internally like that, it''s too fragile and liable to bugs and errors as it isn''t type safe. You can use something like restsharp if this is a REST service you''re calling, but I''d recommend switching to WCF services.


这篇关于Var返回的值为{"addnewuserresult":40}.我如何从中得到40并使用此值.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆