-1

Could someone tell me how i request json from a php file located on another server that gives it to our asp.net?

It includes ~10 variables we want to use again.

I don't want just a string, but an array. so this wont work:

//We don't actually use the given url of course

string url = "url";
var json = new WebClient().DownloadString(url);
Response.Write(json);

I'd like to use it somewhere like this jsonname[2]

chameLEON
  • 21
  • 7
  • Why doesn't that code work? What is the value of `json`? If it's JSON data, can't you deserialize it into an object? – David Dec 14 '17 at 13:12
  • Once you've got your JSON string downloaded, if you want to interpret it as an array and use it in your code then you need to deserialise it. But your URL looks like it includes a JSONP callback, which will only work from an ajax call. Remove that and just call http://ip.jsontest.com. Then you'll get a JSON object back and not a Javascript snippet. – ADyson Dec 14 '17 at 13:20
  • So someone created this code a few years back for our website. It puts some data into a table and echo's it. Now we need to recreate some things for our new website in ASP.NET instead of php. Instead of recreating the whole php code, I want to get the variables from that php code send to me with json. It will contain around 20 variables like : 'productname' and 'price'. – chameLEON Dec 14 '17 at 13:20
  • " I want to get the variables from that php code send to me with json.". Does the PHP code offer that possibility? Or does it just give you some HTML? And what does the code above (fetching IP addresses) have to do with it? Please post something actually relevant. – ADyson Dec 14 '17 at 13:23
  • I see you updated it. Ok. So _if_ the remote site is sending you JSON already, then yeah like I said already you need to deserialise it into a C# object/array. If it's returning HTML, and you can't modify the PHP, then you'll have to parse the HTML and extract the data. – ADyson Dec 14 '17 at 13:24
  • 1
    @chameLEON: None of that last comment of your is relevant to the problem at hand. Do you have a JSON string? Do you want to deserialize that JSON string into an object? When you search for something like "C# deserialize JSON" on Google, what do you find? Have you tried any of that? – David Dec 14 '17 at 13:49

1 Answers1

2

Output a JSON array from your php page like this

$myArr = array("John", "Mary", "Peter", "Sally");
$myJSON = json_encode($myArr); 
header('Content-Type: application/json');
echo $myJSON;

In your c# code you then need to deserialize the string returned by DownloadString into a type that matches the format of the JSON data. If your JSON is just an array of strings, then use JsonConvert.DeserializeObject<List<string>>(json) from the Newtonsoft.Json package. Otherwise you need to replace List with something that matches the Json data, for example a new class with all the same property names and types. In this case

string url = "url";
var json = new WebClient().DownloadString(url);
Response.Write(json);
var myArr = JsonConvert.DeserializeObject<List<string>>(json);
Response.Write(myArr[1]); // will output "Mary"

If you don't have Newtonsoft.Json, this question has some options for obtaining it

weew
  • 363
  • 3
  • 12
  • So let's say this is my php code `` How would I do that? – chameLEON Dec 14 '17 at 13:43
  • Can I see the JSON data rather than the php code that generates it? – weew Dec 14 '17 at 13:44
  • So, I have never done this before and I think that I forgot something. Could you help me with that? – chameLEON Dec 14 '17 at 13:52
  • 1
    If your url just outputs $myJSON then you should be able to use the exact code in my answer to return a List in your c# code. The important thing here is what is actually output by the php page. If you paste the url into your browser, what shows on the screen? – weew Dec 14 '17 at 13:56
  • it doesn't show anything unless I type echo $myJSON; – chameLEON Dec 14 '17 at 14:12
  • 1
    Sorry your question made it sound like the php page was already set up to output the json data. Yes your php page should set its header with `header('Content-Type: application/json');` and must output the json data with `echo $myJSON;` – weew Dec 14 '17 at 14:16
  • okay, that works now. The response I get is ["John", "Mary", "Peter", "Sally"] Sorry for the confusion. I now output it in my asp with Response.Write(json); How Do i only output "Mary" for example? – chameLEON Dec 14 '17 at 14:22