I have a simple button that runs a script to pull data from 40 tables, within the php these are encoded in JSON.
我有一个简单的按钮,它运行一个脚本来从40个表中提取数据,在php中这些是用JSON编码的。
$stmt = $pdo->prepare("SELECT* FROM form");
$stmt->execute();
$results=$stmt->fetchAll(PDO::FETCH_ASSOC);
$form=$results;
$stmt = $pdo->prepare("SELECT * FROM applicant");
$stmt->execute();
$results=$stmt->fetchAll(PDO::FETCH_ASSOC);
$applicant=$results;
print json_encode(array($form,$applicant));
I am not using select * it is just for easy reading. this displays fine as an array , however when im pulling the array into C# this is done as a string, how do i pass an array from php to c#?
我没有使用select *它只是为了方便阅读。这显示为一个数组,但是当我将数组拉入C#时,这是一个字符串,如何将数组从php传递给c#?
WebClient wc = new WebClient();
var json = wc.DownloadString("http://localhost/returnData.php");
List<Applicant> app = JsonConvert.DeserializeObject<List<Applicant>>(json);
List <form> form = JsonConvert.DeserializeObject<List<form>>(json);
dataGrid.ItemsSource = form;
dataGrid2.ItemsSource = app;
Idea 1 This could be bad practise but could i run a script for each table and then just return each JSON object individually, i know this will work but will this be performance heavy and will have to create multiple scripts.
想法1这可能是不好的做法,但我可以为每个表运行一个脚本,然后单独返回每个JSON对象,我知道这将工作,但这将是性能沉重,将不得不创建多个脚本。
[{"id":"1","name_id":"0","first":"advert","last":"","address":"","postcode":"2","date_created":"2016-08-09 15:50:12"},{"id":"2","name_id":"0","first":"advert","last":"","address":"","postcode":"2","date_created":"2016-08-09 16:25:04"}]
Idea 2 i could create one script but return all with a separator. for example print $form."^".$app;
then split them into an array in C#?
想法2我可以创建一个脚本,但使用分隔符返回所有脚本。例如print $ form。“^”。$ app;然后将它们分成C#中的数组?
Idea 3 i would ideally like to pass the array of json arrays to c#.
想法3我理想地想将json数组的数组传递给c#。
2 个解决方案
#1
2
That wc.DownloadString("http://localhost/returnData.php")
code line will try and download the entire resource returnData.php
which will not only include the printed JSON array but as well that page HTML markup and thus your deserialization to JsonConvert.DeserializeObject<List<Applicant>>(json)
logically should fail.
那个wc.DownloadString(“http://localhost/returnData.php”)代码行将尝试下载整个资源returnData.php,它不仅包括打印的JSON数组,还包括页面HTML标记,因此你的反序列化JsonConvert.DeserializeObject
>(json)在逻辑上应该失败。
You should rather try and get those data directly from your C# code (OR) expose those method as Web API method (I mean REST service) and call them from your C# code to get the required data.
您应该尝试直接从C#代码中获取这些数据(OR)将这些方法公开为Web API方法(我的意思是REST服务),并从C#代码中调用它们以获取所需的数据。
#2
0
Try this :
尝试这个 :
Please create a class like this //Jsonget.cs
请创建一个这样的类//Jsonget.cs
public static string jsonconvert(string url)
{
string currentsite = HttpContext.Current.Request.Url.Authority;
WebClient wc = new WebClient();
wc.Encoding = Encoding.UTF8;
wc.Encoding = UTF8Encoding.UTF8;
string test = "http://" + currentsite + url;
var data = wc.DownloadString(test);
string jsonresult = "{\"results\":" + data.ToString() + "}";
return jsonresult;
}
string jsonurl = "";
string getjsonresult = "";
getjsonresult = Jsonget.jsonconvert("http://localhost/returnData.php");
Newtonsoft.Json.Linq.JObject Result= Newtonsoft.Json.Linq.JObject.Parse(getjsonresult);
foreach (var get_result in Result["results"])
{
string id= (string)get_result ["id"];
string name_id= (string)get_result ["name_id"];
}
#1
2
That wc.DownloadString("http://localhost/returnData.php")
code line will try and download the entire resource returnData.php
which will not only include the printed JSON array but as well that page HTML markup and thus your deserialization to JsonConvert.DeserializeObject<List<Applicant>>(json)
logically should fail.
那个wc.DownloadString(“http://localhost/returnData.php”)代码行将尝试下载整个资源returnData.php,它不仅包括打印的JSON数组,还包括页面HTML标记,因此你的反序列化JsonConvert.DeserializeObject
>(json)在逻辑上应该失败。
You should rather try and get those data directly from your C# code (OR) expose those method as Web API method (I mean REST service) and call them from your C# code to get the required data.
您应该尝试直接从C#代码中获取这些数据(OR)将这些方法公开为Web API方法(我的意思是REST服务),并从C#代码中调用它们以获取所需的数据。
#2
0
Try this :
尝试这个 :
Please create a class like this //Jsonget.cs
请创建一个这样的类//Jsonget.cs
public static string jsonconvert(string url)
{
string currentsite = HttpContext.Current.Request.Url.Authority;
WebClient wc = new WebClient();
wc.Encoding = Encoding.UTF8;
wc.Encoding = UTF8Encoding.UTF8;
string test = "http://" + currentsite + url;
var data = wc.DownloadString(test);
string jsonresult = "{\"results\":" + data.ToString() + "}";
return jsonresult;
}
string jsonurl = "";
string getjsonresult = "";
getjsonresult = Jsonget.jsonconvert("http://localhost/returnData.php");
Newtonsoft.Json.Linq.JObject Result= Newtonsoft.Json.Linq.JObject.Parse(getjsonresult);
foreach (var get_result in Result["results"])
{
string id= (string)get_result ["id"];
string name_id= (string)get_result ["name_id"];
}