基本數(shù)據(jù)
-->asp.net
using System;
using System.Collections.Generic;
using System.Text;
namespace WindowsFormsApplication1
{
public class TJSONDataDemo
{
public TJSONDataDemo() { }
public TResult[] result;
}
public class TResult
{
public string type;
public Int32 id;
public TRecList fields;
}
public class TRecList
{
public TRecObj[] MyRecList;
}
public class TRecObj
{
public string type;
public Int32 id;
public TFieldList fields;
}
public class TFieldList
{
//public Array[] Arr;
public string Name;
public Int32 Age;
public double Birthday;
public double Tall;
}
/*
*/
}
-->Delphi Xe2
type
TMyRec= class
private
Name: string;
Age: Integer;
Birthday: TDateTime;
Tall: Double;
end;
TMyClassDemo= class
private
MyRecList: array of TMyRec;
end;
-->asp.net --->datasnap_rest
#region "發(fā)收測(cè)試"
//webReq.ContentLength = byteArray.Length;
//Stream newStream = webReq.GetRequestStream();
//newStream.Write(byteArray, 0, byteArray.Length);//寫入?yún)?shù)
//newStream.Close();
webReq.UseDefaultCredentials = true;
string username = "admin";
string userPass = "admin";
string code = Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", username, userPass)));
webReq.Headers.Add("Authorization", "Basic " + code);
HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default);
ret = sr.ReadToEnd();
TJSONDataDemo jDemo = JsonMapper.ToObject<TJSONDataDemo>(ret);
textBox1.Text = ret;
sr.Close();
response.Close();
//newStream.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/// <summary>
/// 發(fā)送JSON數(shù)據(jù) Demo
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
string ret = string.Empty;
string paramData = string.Empty;
TRecList recList= new TRecList();
recList.MyRecList= new TRecObj[3];
for (int i = 0; i < recList.MyRecList.Length; i++)
{
recList.MyRecList[i] = new TRecObj();
recList.MyRecList[i].id = i;
recList.MyRecList[i].fields = new TFieldList();
recList.MyRecList[i].fields.Age = i;
recList.MyRecList[i].fields.Birthday = 12.22;
recList.MyRecList[i].fields.Name = "姓名"+ i.ToString();
recList.MyRecList[i].fields.Tall = 1223.33;
}
paramData = JsonMapper.ToJson(recList);
//webReq.Credentials = CredentialCache.DefaultCredentials;
webReq.UseDefaultCredentials = true;
string username="admin";
string userPass= "admin";
string code = Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", username, userPass)));
webReq.Headers.Add("Authorization", "Basic " + code);
/*
request.Credentials = CredentialCache.DefaultCredentials;
//獲得用戶名密碼的Base64編碼
string code = Convert.ToBase64String(Encoding.ASCII.GetBytes(string.Format("{0}:{1}", userName, password)));
//添加Authorization到HTTP頭
request.Headers.Add("Authorization", "Basic " + code);
*/
//webReq.AuthenticationLevel= System.Net.Security.AuthenticationLevel.
webReq.ContentLength = byteArray.Length;
Stream newStream = webReq.GetRequestStream();
newStream.Write(byteArray, 0, byteArray.Length);//寫入?yún)?shù)
newStream.Close();
HttpWebResponse response = (HttpWebResponse)webReq.GetResponse();
StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.Default);
ret = sr.ReadToEnd();
//TJSONDataDemo jDemo = JsonMapper.ToObject<TJSONDataDemo>(ret);
textBox1.Text = ret;
sr.Close();
response.Close();
//newStream.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
#endregion
-->Xe2_rest 服務(wù)器
...驗(yàn)證
procedure TServerContainer1.TDSAuthenticationManagerUserAuthenticate(Sender: TObject;
const Protocol, Context, User, Password: string; var valid: Boolean;
UserRoles: TStrings);
begin
//
if (User='admin') and (Password='admin') then
valid:= true
else valid:= False;
end;
...處理查詢
function TServerMethods1.JsonMarshallDemo: TJSONValue;
var
myClsDemo: TMyClassDemo;
i: Integer;
jm: TJSONMarshal;
jv: TJSONValue;
begin
myClsDemo:= TMyClassDemo.Create;
SetLength(myClsDemo.MyRecList,10);
for i := 0 to 9 do begin
myClsDemo.MyRecList[i]:= TMyRec.Create;
myClsDemo.MyRecList[i].Name:= '姓名'+ IntToStr(i);
myClsDemo.MyRecList[i].Age:= i;
myClsDemo.MyRecList[i].Birthday:= now;
myClsDemo.MyRecList[i].Tall:= i+ 1.2;
end;
jm:= TJSONMarshal.Create();
jv:= jm.Marshal(myClsDemo);
jm.Free;
myClsDemo.Free;
Result:= jv;
end;
...JSON提交
function TServerMethods1.updateJsonDemo(field: string;
data: TJSONObject): string;
begin
//
Result:= field;
// Result:= Result+ data
// ;
if data.Null then begin
Result:= Result+ ' Data is null';
end else begin
Result:= Result+ ' data is not null';
result:= Result+ data.ToString();
end;
end;