0

I use WebClient in first time. And when I do the following

var raw = new WebClient().DownloadString("http://ip-api.com/json");

and just test-method show it normally in browser as string:

 public string GetLocationData()
 {
        var raw = new WebClient().DownloadString("http://ip-api.com/json");
        return raw;
 }

However, real-need method returns with extra backslashes

public ActionResult GetLocationDataInJson()
{
        return Json(GetLocationData(), JsonRequestBehavior.AllowGet);
}

returns me

"{\"as\":\"AS6584 Microsoft Corporation\",\"city\":\"Dublin\",\"country\":\"Ireland\",\"countryCode\":\"IE\",\"isp\":\"Microsoft Limited\",\"lat\":53.3331,\"lon\":-6.2489,\"org\":\"Microsoft Corporation\",\"query\":\"194.69.98.180\",\"region\":\"L\",\"regionName\":\"Leinster\",\"status\":\"success\",\"timezone\":\"Europe/Dublin\",\"zip\":\"\"}"

So I can't use this data to proccess via MicrosoftJson. How can I get normal Json data? I tried String.Replace() but it doesnt work.

Updates : the same happens when I try to send my raw string to another method

public class LocationData
{
    public LocationData(string json)
    {
        JObject jObject = JObject.Parse(json);
        JToken location = jObject["LocationData"];
        Latitude = (string)location[@"lat"];
        Longitude = (string)location[@"lon"];
    }

    public string Latitude { get; set; }
    public string Longitude { get; set; }

}

it throw an exception and says that can't find "lat" or "lon"; Debugger shows this input string in constructor method of LocationData with backslashes.

I read another questions about verbatim string and usage of "@". It doesn't work.

Jim Hewitt
  • 1,726
  • 4
  • 24
  • 26
Iskander Raimbaev
  • 1,322
  • 2
  • 17
  • 35
  • 1
    You are already downloading a JSON object, so wrapping it in `Json()` again will JSON-escape everything, which is what you're seeing (escaped quotes within a quoted string etc.). You might want to return the raw string. Try `return Content(GetLocationData()));`. – Maximilian Gerhardt Sep 25 '16 at 07:30
  • 1
    @MaximilianGerhardt Oh, wait... If the returned string is actually what the OP is getting, rather than what the debugger is showing, then it's not a duplicate of what I had closed it as. Re-opened. –  Sep 25 '16 at 07:34
  • 1
    Either the double-escaping comes from the `Json()` call on an already JSON-encoded object or it's what the debugger shows him, in which case there is no problem. – Maximilian Gerhardt Sep 25 '16 at 07:38
  • 1
    This doesn't answer your question, but you shouldn't chain `new WebClient().DownloadString` as `WebClient` is `IDisposable`. You should explicitly dispose of it after use. – Enigmativity Sep 25 '16 at 07:41
  • 1
    Can you please provide a [mcve] that replicates this issue? – Enigmativity Sep 25 '16 at 07:45
  • @MaximilianGerhardt, i checked. 1. Json data on server works well. When I access via browser, there are no problems. 2. WebClient().DownloadString() converts this Json format into string. When this string converted again to Json by GetLocationDataInJson - here backslashes appears - in debugger and in actual data showed on browser. Is there any way to download data from server in pure-json format? – Iskander Raimbaev Sep 25 '16 at 07:47
  • @Enigmativity, should I add it as extra text on question or totally rewrite it? – Iskander Raimbaev Sep 25 '16 at 07:48
  • 3
    1) If you are observing this string inside Visual Studio debugger, it will escape backslashes. This doesn't mean the backslashes are actually there. Print the string to the console or to the log file. 2) If the `Json` method *serializes* the object (and the "object" you passed is a *string*, because `GetLocationData()` already returns a JSON serialized string), then Json will just return a single string with escaped backslashes. Your `GetLocationData()` should probably be named `GetLocationDataInJson()`, because (presumably) it's what it already does. – vgru Sep 25 '16 at 08:02
  • 1
    @IskanderRaimbayev - You should write enough code for us to copy, paste, and run so that we can see the error. Let us know what libraries/references you need to run the code. – Enigmativity Sep 25 '16 at 08:04
  • 1
    If I literally input your string (`"{\"as\":\"AS6584 Microsoft Corporation\" [...]`) in code and replace `(string)location[@"lat"];` by `Latitude = (string)jObject["lat"];` it works without problem. Why do you think that `JToken location = jObject["LocationData"];` would return anything useful? – grek40 Sep 25 '16 at 08:27
  • 2
    You have a (valid) JSON string and want to return that from the MVC method. Using `Json(object)` will serialize the object you pass in. So in your case, the *string* is being serialized (although it already is a JSON string—so you get a JSON string of a JSON blob). You can use `Content()` with the correct MIME type instead if you want to return the raw JSON string as shown in the linked question. An alternative would be to parse the JSON first and then serialize it back as JSON; but unless you actually need the deserialized object, this does more work than necessary. – poke Sep 26 '16 at 06:23

1 Answers1

2

I think you can try this method

 var raw = new WebClient().DownloadString("http://ip-api.com/json");

  var json = JToken.Parse(raw.ToString());

  string Latitude = (string)json[@"lat"];
  string Longitude = (string)json[@"lon"];
Justin CI
  • 2,693
  • 1
  • 16
  • 34