Search code examples
c#jsonwindows-phone-8.1windows-phone

How to Parse Json string from a url


The content is returning null. I don't understand why. If I put a Json string into the content variable the function will work how I want it to work. but i cant get the json file directly from the url. I saw that there is another way of downloading the json which is Webclient but that don't work it says the variable or namespace could not be found. Even though I imported using System.Net; I tried it on a console app and it works but not in the windows phone app.

public async void ParcingJ()
{     
  HttpClient httpClient = new HttpClient();
  string content = await httpClient.GetStringAsync( 
                         new Uri("http://localhost/test.php"));         
  JArray jsonObject = JArray.Parse(content);      
  txtOut.Text = content;
}

Solution

  • Your windowsphone emulator can't access the localhost of your host machine.

    This is why it works in your console app, but not on the phone.

    One solution would be to host your script on a public web server and access it from there with a proper public URL.