精通
英语
和
开源
,
擅长
开发
与
培训
,
胸怀四海
第一信赖
锐英源精品开源心得,转载请注明:“锐英源www.wisestudy.cn,孙老师作品,电话13803810136。”需要全文内容也请联系孙老师。
这篇文章对于那些将使用ASP.NET C#Web服务/ Web API Services开发项目的人来说非常有用。本文对于正在使用Xamarin开发项目的人员也是有用的,可用于移动应用开发和应用创建
本文简化了你的事情。您只需要准备您的服务URL和发布数据(如果发布服务)。
那么最后,在这里你会得到json字符串作为输出,只需要根据你的json格式的字符串创建类,并根据你的要求使用数据。
移动应用程序开发人员可以使用所有这些方法,只是他们不能使用datatable只能用list来执行其操作。
为此,您将需要下载ImportJson.dll,RestSharp.dll和Newtonsoft dll。在您的项目中添加ImportJson DLL 的引用。
如何添加?
下载ImportJson:
例如GET获取访问权限的服务令牌
URL:http : //test.mydomin.com/ipos/oauth/api/?grantType=password&username=userName&password=Password@321
使用Json输出功能,如下所示:
///<summary>
/// Simple Get method
///</summary>
///<returns> Json formatted data </returns>
public string GetJsonData1()
{
IOperations _Obj = ClsOperations.GetOperations();
string url = "http://test.mydomin.com/ipos/oauth/api/
?grantType=password&username=userName&password=Password@321";
string jsonResult = _Obj.GetJsonResult(url);
return jsonResult;
}
上面的函数将返回json格式string如下:
{
"success": true,
"count": 1,
"totalCount": 1,
"access_token": "70f1f689-457e-49da-8858-87f478240051",
"errors": null
}
如何使用:假设我要将访问令牌分配给标签文本,然后创建类如下:
public class ClsAccessToken
{
public bool success { get; set; }
public int count { get; set; }
public int totalCount { get; set; }
public string access_token { get; set; }
public object errors { get; set; }
}
string _res = GetJsonData1();
ClsAccessToken obj = (JsonConvert.DeserializeObject<ClsAccessToken>(_res));
现在你将从类对象中获得每个和所有的东西。以下代码将会将访问令牌值并分配给标签。
Label1.Text = obj.access_token;
ID |
名称 |
市 |
1 |
Amol |
Buldana |
2 |
Ram |
Nagpur |
3 |
Krishna |
Amaravati |
现在假设你的json数据有如下多个或表格格式的数据:
[{
"ID": "1",
"Name": "Amol",
"City": "Buldana"
}, {
"ID": "2",
"Name": "Ram",
"City": "Nagpur"
}, {
"ID": "3",
"Name": "Krishna",
"City": "Amaravati"
}]
你想要写入List或显示到Gridview上,所以程序如下:
创建类并获取详细信息:
public class ClsEmployee
{
public string ID { get; set; }
public string Name { get; set; }
public string City { get; set; }
}
protected void GetData()
{
string url = "http://test.mydomin.com/ipos/oauth/api/GetEmployeeData";
IOperations obj = ClsOperations.GetOperations();
IEnumerator enumerator = obj.GetJsonEnumerableResult(url);
List<ClsEmployee> lst = new List<ClsEmployee>();
while (enumerator.MoveNext())
{
lst.Add(Newtonsoft.Json.JsonConvert.DeserializeObject
<ClsEmployee>(enumerator.Current.ToString()));
}
// Mobile developers use the lst as list of List<ClsEmployee>
DataTable dt = CommonServiceCall.ToDataTable(lst);
// For list to datatable take reference from internet
// E.g. http://stackoverflow.com/questions/18100783/how-to-convert-a-list-into-data-table
//Dot net developers can perform operations on datatable
}
如果您想仅显示特定员工的详细信息,例如,从上述示例中,我们将仅显示第一个员工的信息ID=1。
ID |
名称 |
市 |
1 |
Amol |
Buldana |
protected void GetDataByParameter()
{
string url = "http://test.mydomin.com/ipos/oauth/api/GetEmployeeData";
IOperations obj = ClsOperations.GetOperations();
Dictionary<string, object> objDec = new Dictionary<string, object>();
objDec.Add("@ID", "1");
IEnumerator enumerator = obj.GetJsonEnumerableResult(url,objDec );
List<ClsEmployee> lst = new List<ClsEmployee>();
while (enumerator.MoveNext())
{
lst.Add(Newtonsoft.Json.JsonConvert.DeserializeObject
<ClsEmployee>(enumerator.Current.ToString()));
}
DataTable dt = CommonServiceCall.ToDataTable(lst);
}
ID |
名称 |
市 |
1 |
Amol |
Buldana |
///<summary>
/// Post Method with Input/ data to post in JSON format
///</summary>
///<returns> Json formatted data </returns>
protected void GetDataByjsonParameter()
{
string url =
"http://test.mydomin.com/ipos/oauth/api/GetEmployeeData";
IOperations obj = ClsOperations.GetOperations();
string InputJson = "{ \"ID\": \"1\" }";
IEnumerator enumerator = obj.GetJsonEnumerableResult(url, null ,InputJson );
List<ClsEmployee> lst = new List<ClsEmployee>();
while (enumerator.MoveNext())
{
lst.Add(Newtonsoft.Json.JsonConvert.DeserializeObject
<ClsEmployee>(enumerator.Current.ToString()));
}
DataTable dt = CommonServiceCall.ToDataTable(lst);
}
ID |
名称 |
市 |
1 |
Amol |
Buldana |
protected List<ClsEmployee> GetDataByjsonParameterXamrine()
{
string url =
"http://test.mydomin.com/ipos/oauth/api/GetEmployeeData";
IOperations obj = ClsOperations.GetOperations();
JSONObject JSONObj = new JSONObject();
JSONObj.put("ID", "1");
string InputJson = JSONObj.ToString();
IEnumerator enumerator = obj.GetJsonEnumerableResult(url, null, InputJson);
List<ClsEmployee> lst = new List<ClsEmployee>();
while (enumerator.MoveNext())
{
lst.Add(Newtonsoft.Json.JsonConvert.DeserializeObject
<ClsEmployee>(enumerator.Current.ToString()));
}
return lst;
}
在这里,您可以看到,您不必担心GET或POST方法,默认情况下,如果单个URL返回数据意味着服务是GET类型,并且如果具有参数以及URL POST。但是有些时候这可能发生,具有单一URL的服务仍然是POST类型。在这种情况下,我们可以使用如下功能:
public string GetJsonDataByForcefullyPOST()
{
IOperations _Obj = ClsOperations.GetOperations();
string url = "http://test.mydomin.com/ipos/oauth/api/
?grantType=password&username=userName&password=Password@321";
string jsonResult = _Obj.GetJsonResult (url,null,null,ServiceType.POST );
return jsonResult;
}
亲爱的朋友们,
希望前文对你非常有帮助,现在我将告诉你如何使用上面的代码来处理各种类型的JSON格式的数据和使用它,所以在开发时你不需要从外部引用所有的资料都可以在这里找到。
{ "ID": "1", "Name": "Amol Khandagale", "City": "Buldana" }
然后它可以被处理和使用如下:
//Handling simple JSON protected void HandleSimpleJSON() { string url = "http://test.mydomin.com/ipos/oauth/api/GetEmployeeData"; IOperations obj = ClsOperations.GetOperations(); ClsEmployee objEmp = new ClsEmployee(); String _res=obj.GetJsonResult(url); objEmp = Newtonsoft.Json.JsonConvert.DeserializeObject<ClsEmployee>(_res); string EmpID = objEmp.ID; // Gives result as 1 string EmpName = objEmp.Name; // Gives result as Amol string EmpCity = objEmp.City; // Gives result as Buldana }
如果JSON格式如下:
{ "ID": "1", "Name": "Amol", "City": "Buldana" }, { "ID": "2", "Name": "Ram", "City": "Nagpur" }, { "ID": "3", "Name": "Krishna", "City": "Amaravati" } ]
然后它可以被处理和使用如下:
//Handling simple JSON object having multiple values
protected void GetAllEmployeeDetails()
{
string url = "http://test.mydomin.com/ipos/oauth/api/GetEmployeeData";
IOperations obj = ClsOperations.GetOperations();
IEnumerator enumerator = obj.GetJsonEnumerableResult(url);
List<ClsEmployee> lst = new List<ClsEmployee>();
while (enumerator.MoveNext())
{
lst.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<ClsEmployee>(enumerator.Current.ToString()));
}
/* Here we get List of Employees, you can convert it to DataTable , IEnumerable, LINQ object and can perform various operations on it. */
}
如果JSON格式如下:
{"Status": "Success",
"Message": "Success",
"TotalCount": "3",
"Data": [{
"ID": "1",
"Name": "Amol",
"City": "Buldana"
}, {
"ID": "2",
"Name": "Ram",
"City": "Nagpur"
}, {
"ID": "3",
"Name": "Krishna",
"City": "Amaravati"
}]
}
然后它可以被处理和使用如下:
现在这里的JSON对象很复杂,你可以说它是一个嵌套对象。您只需要了解有多少个嵌套对象,并且必须相应地创建类。在这个例子中,我们有两个对象1)包含四个实体的对象i)状态ii)消息iii)
TotalCount iv)数据
“数据”实体再次包含三个实体,即i)ID ii)名称iii)城市
因此,我们需要两个类:
public class ClsEmployee{
public string ID { get; set; }
public string Name { get; set; }
public string City { get; set; }
}
public class ClsEmployeeDetails
{
public string Status { get; set; }
public string Message { get; set; }
public string TotalCount { get; set; }
public List< ClsEmployee > Data { get; set; }
}
//Handling little JSON object having multiple values
protected DataTable GetEmployeeDetails()
{
string url = "http://test.mydomin.com/ipos/oauth/api/GetEmployeeData";
IOperations obj = ClsOperations.GetOperations();
ClsEmployee objEmp = new ClsEmployee();
String _res=obj.GetJsonResult(url);
ClsEmployeeDetails objEmpData = JsonConvert.DeserializeObject< ClsEmployeeDetails >(_res);
List< ClsEmployee> lst1 = new List< ClsEmployee >();
if (objEmpData.data != null)
{
foreach (ClsEmployee i in objEmpData.data)
{
lst1.Add(i);
}
}
}
如果JSON格式如下:
{ "Status": "Success","Message": "Success",
"TotalCount": "3",
"Data": [{
"ID": "1",
"Name": "Amol",
"City": "Buldana",
"Skills": [{
"Subject": "ASP.NET"
}, {
"Subject": "C#"
}]
}, {
"ID": "2",
"Name": "Ram",
"City": "Nagpur",
"Skills": [{
"Subject": "C#"
}]
}, {
"ID": "3",
"Name": "Krishna",
"City": "Amaravati",
"Skills": [{
"Subject": "ASP.NET"
}, {
"Subject": "C#"
}, {
"Subject": "SQL Server"
}]
}]
}
现在这里再次嵌套增加,因此结构变得很复杂。在员工细节中,有多名员工和每位员工都有多种技能,现在我们需要创建三个类:
public class ClsSkills{
public string Subject { get; set; }
}
public class ClsEmployee
{
public string ID { get; set; }
public string Name { get; set; }
public string City { get; set; }
public List<ClsSkills> Skills { get; set; }
}
public class ClsEmployeeDetails
{
public string Status { get; set; }
public string Message { get; set; }
public string TotalCount { get; set; }
public List<ClsEmployee> Data { get; set; }
}
这里用List容器类建立了复合关系,然后它可以被处理和使用如下:
protected void GetEmployeeDetails()
{
string url = "http://test.mydomin.com/ipos/oauth/api/GetEmployeeData";
IOperations obj = ClsOperations.GetOperations();
ClsEmployee objEmp = new ClsEmployee();
String _res=obj.GetJsonResult(url);
ClsEmployeeDetails objData = JsonConvert.DeserializeObject<ClsEmployeeDetails>(GetJsonString());
List<ClsEmployee> lst = new List<ClsEmployee>();
List<ClsSkills> lst1 = new List<ClsSkills>();
if (objData.Data != null)
{
foreach (ClsEmployee e in objData.Data)
{
lst.Add(e); foreach (ClsSkills i in e.Skills ) { lst1.Add(i); } } } /* Now we can make use of these lists lst, lst1, objData and can perform your required operations */ }
这样,只需参考这个嵌套,并希望您可以处理任何JSON数据格式。List容器类能够方便灵活地添加多个对象。
如果您正在使用ASP.Net,C#和Web服务或Xamarine应用程序开发开发应用程序,本文将在整个项目中帮助您。