How to parse JSON in windows 8

I am doing winJS.xhr like this:

var jsonResult; WinJS.xhr( { url: urlGoogle, responseType: 'json' } ).done(function complete(response) { jsonResult = response.responseText; console.log(jsonResult); }, //Error and Progress functions ); 

The console log shows me the following:

 {lhs: "32 Japanese yen",rhs: "0.30613818 Euros",error: "",icc: true} 

And I want to get information about rhs. So I tried to do

 console.log(jsonResult.rhs); 

and

 console.log(jsonResult['rhs']); 

It shows me only "undefined". Then I realized that when I did jsonResult [0], it shows me the first character (which is {), etc. Using an index bracket.

I tried to do JSON.parse (jsonResult); but he creates an error

 json parse unexpected character 
+4
source share
6 answers
 var test = {lhs: "32 Japanese yen",rhs: "0.30613818 Euros",error: "",icc: true} //test.lhs returns "32 Japanese yen" 

I do not quite understand why this does not work for you. Try writing console.log(typeof jsonResult) to see if jsonResult is a string or an object . (if it were a string, I would say that JSON.parse should work)
Then log jsonResult and see if you can go through its properties. (The Google Chrome console works like a charm for this)

In case this is a string, this is a (somewhat hacky, unsafe) way to do this:

 var result = eval('({lhs: "32 Japanese yen",rhs: "0.30613818 Euros",error: "",icc: true})') var result = eval('(' + jsonResult + ')') 

(Thanks to @ThiefMaster ♦ for a more proper (-ish) use of eval instead of my own abuse of it.)
Then you can access result
Generally, you do not want to use eval , but if all else fails ...

+1
source

The string you see is actually not valid JSON, as property names are not quoted. This is why JSON.parse throws an error.

+6
source

Just tried in Chrome Dev Tools:

 JSON.parse("{lhs: \"32 Japanese yen\",rhs: \"0.30613818 Euros\",error: \"\",icc: true}") SyntaxError: Unexpected token l JSON.parse('{lhs: "32 Japanese yen",rhs: "0.30613818 Euros",error: "",icc: true}') SyntaxError: Unexpected token l JSON.parse('{lhs: "32 Japanese yen",rhs: "0.30613818 Euros",error: "",icc: 1}') SyntaxError: Unexpected token l JSON.parse('{"lhs": "32 Japanese yen","rhs": "0.30613818 Euros","error": "","icc": true}') Object error: "" icc: true lhs: "32 Japanese yen" rhs: "0.30613818 Euros" __proto__: Object 

Thus, it seems that the "valid" JSON string should use a double quote " to enclose all possible places.

Actually this also happens in PHP json_decode .

I don't know about Win8JS development, so I'm not sure if you can use response.responeJSON or something like that, but the direct analysis of response.responseText seems unsuccessful.

If you really need to use responseText , consider the dangerous eval @Cerbrus method.

+2
source

In your case, check the following

 WinJS.xhr({ url: urlGoogle }).then( function completed(response) { var jsonString = JSON.parse(response.responseText); console.log(jsonString .rhs); }, function error(error) { console.log(error) }, function progress(progress) { } ); OR WinJS.xhr({ url: urlGoogle }).then( function completed(response) { var jsonString = JSON.parse(response.responseText); console.log(jsonString .rhs); }, function error(error) { console.log(error) }, function progress(progress) { } ); 
+1
source

First do:

 jsonResult = JSON.parse(response.responseText); 

and then you can use:

 var rhs = jsonResult.rhs; 
0
source

In the past, I wrote about this on my blog. The code below calls a web service that returns JSON.

Explained here: http://blogs.msdn.com/b/brunoterkaly/archive/2012/11/06/step-4-augmented-reality-windows-8-and-cloud-computing-how-to-implement-with -real-code-implementing-the-windows-8-client.aspx #

What is useful in this code is that you are "trying" to get the values. They may not exist.

See parsedResults.TryGetValue ().

  private async System.Threading.Tasks.Task CallLocationWebService(string gps) { // Call into the emulator. This assumes you are running the // cloud project from the last post in the backgruond string _location = "http://127.0.0.1:81/api/values?location={0}"; // You can use the line below once you deploy your cloud // application to the cloud (a MS data center) //string _location = "http://locationwebservice.cloudapp.net/api/values?location={0}"; // Now make the aynchronous call. We need to pass the GPS // parameters here to the _location string mentioned above. using (HttpClient clientlocation = new HttpClient()) using (var response = await clientlocation.GetAsync(string.Format(_location, gps))) { if (response.IsSuccessStatusCode) { string webresponse = await response.Content.ReadAsStringAsync(); // Parse the string into a JSONObject var parsedResults = JsonObject.Parse(webresponse); IJsonValue val; // Extract data embedded in JSONObject. // Assign to controls in user interface if (parsedResults.TryGetValue("latitude", out val)) loc_info.latitude = val.GetString(); if (parsedResults.TryGetValue("longitude", out val)) loc_info.longitude = val.GetString(); if (parsedResults.TryGetValue("bus_and_neighborhood", out val)) loc_info.bus_and_neighborhood = val.GetString(); if (parsedResults.TryGetValue("elevation", out val)) loc_info.elevation = val.GetString(); if (parsedResults.TryGetValue("bus_and_neighborhood", out val)) loc_info.bus_and_neighborhood = val.GetString(); if (parsedResults.TryGetValue("max_temp", out val)) loc_info.max_temp = val.GetString(); if (parsedResults.TryGetValue("min_temp", out val)) loc_info.min_temp = val.GetString(); this.bus_and_neighborhood.Text = loc_info.bus_and_neighborhood; this.elevation.Text = loc_info.elevation; this.latlong.Text = loc_info.latitude + "/" + loc_info.longitude; this.max_temp.Text = loc_info.max_temp; this.min_temp.Text = loc_info.min_temp; } } } 
-1
source

Source: https://habr.com/ru/post/1447422/


All Articles