MY CASE
I am trying to connect my Universal Application (UWP) to my website (PHP) which performs queries to my database (MySQL). The results returned by PHP are JSON strings which I have to PARSE in my app through JsonObject .
WHAT WORKS
1. Connection to the website (ok).
2. SQL queries (ok).
3. Return of JSON strings (ok).
THE CODES
JSON strings returned by PHP.
Modelo A: "{'CAT':'LOG','MSG':'OK','SUBB':{'ID':'U1','USU':'JORNY'}}";
Modelo B: "{'CAT':'LOG','MSG':'ER','SUBB':'-'}";
Function that parses the JSON string in C # (UWP)
private async void Consultar(object sender, RoutedEventArgs e) {
/*Capturo los reultados devueltos por PHP*/
string CadenaJSON = await AccessTheWebAsyncPost();
/*reemplazo comillas simples por dobles, esta parte lo hago así
porque C# devuelve error de JSON no válido*/
string contentLength = CadenaJSON.Replace("'", "\"");
/*parseo a traves de JsonObject la cadena*/
JsonObject jsonObject = JsonObject.Parse(contentLength);
/*leo las secciones CAT y SUBB*/
string sCat = jsonObject["CAT"].GetString();
string sSubb = jsonObject["SUBB"].GetString();/*<---- El error ocurre aquí*/
/*muestro los resultados*/
txtCat.Text = sCat.ToString();
txtSubb.Text = sSubb.ToString();
}
WHAT DOES NOT WORK
1. The parsing of JSON strings works very well when PHP returns model B, but when it returns model A, it sends an error.
I guess it's because model A has internal sections within SUBB but model B does not.