Task:
I would like to evaluate a Json return from a website in WPF.
Solution in C #, wpf:
//< convert to array > JsonObject jsonDownload = JsonObject.Parse(sDownload_as_String); JsonValue jsonResponse = jsonDownload["response"] as JsonValue; JsonArray jsonResonseArray = jsonResponse.GetArray(); //</ convert to array >
//----< @Loop: Json-Rows >---- foreach (var jsonRow in jsonResonseArray) { //----< Json_Row >---- JsonObject jsonObject = jsonRow.GetObject();
//< values > string sID = jsonObject["id"].GetString(); string sTitle = jsonObject["name"].GetString(); //</ values > .. } |
WPF requires Windows.Data.Json (Microsoft)
By default you can integrate json via windows.data.json.
using Windows.Data.Json; //*JsonArray |
Translate a json download into values
Example of a Json results page:
[Lnk "https://www.contractor.de/api/?action=getJobsList&type=undefined&keyword" /] =
"success":true,"response":[{"id":"6134","name":"Teilprojektleiter (m\/w) SAP Rollout f\u00fcr 3 Tage\/Woche","place":"N\u00fcrnberg\/Kitzingen","start":"Okto…. |
In the debugger you can see the values, which are loaded at runtime.
/
? |
Name |
Value |
Type |
jsonDownload |
null |
Windows.Data.Json.JsonObject |
|
▶ |
jsonDownload["response"] |
{[{"id":"6134","name":"Teilprojektleiter (m/w) SAP Rollout für 3 Tage/Woche","place":"Nürnberg/Kitzingen","start":"Oktober 2018","dur":"12 Monate+","link":"https://goo.gl/6Exg23"},{"id":"6126","name":"Senior Mobile Developer (m/w) Android & iOS","place":"Raum München","start":"September","dur":"4 Monate+","link":"https://goo.gl/EvkQnA"},{"id":"6125","name":"Solution Architekt (m/w) Microsoft","place":"München","start":"asap","dur":"4 Monate","link":"https://goo.gl/w5UY7Z"},{"i.. |
Windows.Data.Json.IJsonValue {Windows.Data.Json.JsonValue} |
▶ |
jsonRow |
{{"id":"6134","name":"Teilprojektleiter (m/w) SAP Rollout für 3 Tage/Woche","place":"Nürnberg/Kitzingen","start":"Oktober 2018","dur":"12 Monate+","link":"https://goo.gl/6Exg23"}} |
Windows.Data.Json.IJsonValue {Windows.Data.Json.JsonValue} |
▶ |
jsonObject["id"] |
{"6134"} |
Windows.Data.Json.IJsonValue {Windows.Data.Json.JsonValue} |
jsonObject["id"].GetString() |
"6134" |
string |
|
sTitle |
"Teilprojektleiter (m/w) SAP Rollout für 3 Tage/Woche" |
string |
Complete code in C #
public static async Task<bool> Scan_Json(string sURL) { //--------< Scrape_List() >--------
app.URL = sURL; //Uri baseUrl = new Uri(sURL);
if (app.Check_Stop == true) return false;
string sPage = sURL; //baseUrl.Query; clsSys.fx_Log("--< List: " + sPage + " >--");
//< download > string sDownload_as_String = await clsWebReader.Web_Get_Download_as_String(sURL); if (sDownload_as_String == null) return false; //</ download >
try { //------< Get Values as Json-Array >------ //< convert to array > JsonObject jsonDownload = JsonObject.Parse(sDownload_as_String); JsonValue jsonResponse = jsonDownload["response"] as JsonValue; JsonArray jsonResonseArray = jsonResponse.GetArray(); //</ convert to array >
//----< @Loop: Json-Rows >---- foreach (var jsonRow in jsonResonseArray) { //----< Json_Row >---- JsonObject jsonObject = jsonRow.GetObject();
//< values > string sID = jsonObject["id"].GetString(); string sTitle = jsonObject["name"].GetString(); //</ values >
string sURL_Detail = app_settings.scan_List_Json_URL_Base + sID;
//< correct > //sTitle = clsText.Encode(sTitle); //</ correct >
//< add/update URL > clsDB.Add_or_Update_Record_from_List(sURL_Detail, sTitle); //</ add/update URL >
//< print > clsSys.fx_Log(sTitle); //</ print >
//----</ Json_Row >---- } //----</ @Loop: Json-Rows >---- //------</ Get Values as Json-Array >------ } catch (Exception ex) { clsSys.fx_Error_Log(ex.Message);
}
clsSys.fx_Log("--</ List: " + sPage + " >--"); clsSys.fx_Log("");
//< out > return true; //</ out > //--------</ Scrape_List() >-------- }
|