1

I am trying to get json string from a url by using following line of code. When I enter the url in Google Chrome I get whole string and data. but when I use my code it returns only this line of string
{"expand":"projects","projects":[]}
it is exact what I get when I enter the url in IE 10. How can I get same data that I get when I enter the url in Chrome? here is my code to get the json data.
var jsonStr = new WebClient().DownloadString("https_my_url");

user217648
  • 3,338
  • 9
  • 37
  • 61
  • 2
    get the [json.net](http://json.codeplex.com/) library to serialize / deserialize any json you encounter. – Wim Ombelets Sep 13 '13 at 07:40
  • Does the web site/service you are communicating with require any kind of authentication? Without knowing what site/service you are talking to, its quite difficult to help. – Mesh Sep 13 '13 at 07:40
  • @Adrian, I don't see what authentication has to do with the OP's question? – Moo-Juice Sep 13 '13 at 07:43
  • Because the data returned from Chrome is different when returned by WebClient. My theory is that, if the request was authenticated then the data returned would be different. – Mesh Sep 13 '13 at 07:48
  • @user217648 What string do you get back in each case? – Mesh Sep 13 '13 at 07:55
  • Adrian, when I enter the url in chrome I get back a very long string which contins data I want to. but my code and IE10 returns only one line of string which is {"expand":"projects","projects":[]} – user217648 Sep 13 '13 at 08:06
  • It really sounds like Chrome is returning different data. Have you logged on to the site in Chrome? Chrome may be passing an authentication token in a cookie. WebClient and IE may not have this token because you have not logged in. – Mesh Sep 13 '13 at 08:12
  • Adrian, I tested your code it returned same result, only difference was it formated it in three lines instead. same result but it added line break. – user217648 Sep 13 '13 at 08:13
  • Adrian, you are right, I tried to log on to the site in IE. now it returns same as chrome but it prompts to open/save a file instead. – user217648 Sep 13 '13 at 08:16

2 Answers2

1

You will need to authenticate the request via WebClient.

See this answer for how to do that if the site uses Forms Authentication.

WebClient accessing page with credentials

Community
  • 1
  • 1
Mesh
  • 6,262
  • 5
  • 34
  • 53
  • Thank you Adrian, It seems you need to send your username and password to the server, which I am not allowed to do. Is it possible to use coockie by getting jira's logon page once and having some kind of token or something to use? or using oauth? I am trying to find a solution. – user217648 Sep 13 '13 at 09:17
  • I don't know much about JIRA but some searching lead me here http://stackoverflow.com/questions/11869780/jira-rest-api-login-using-c-sharp Also Atalssian/JIRA documentation seems very thorough https://developer.atlassian.com/display/JIRADEV/JIRA+REST+API+Example+-+Basic+Authentication – Mesh Sep 13 '13 at 09:41
0

You need to use a JSON parser to turn it in to something useful. ServiceStack.Text (available via NuGet or download) has an excellent one that can turn json strings in to first-class POCOs.

using ServiceStack.Text;

public sealed class SomeObject
{
    public string expand { get; set; }
    public List<string> projects {get; set; }
}

And convert thusly:

SomeObject object = jsonString.FromJson<SomeObject>();

Note, I would make my POCOs a little more c# friendly and alias away the lower-case:

using ServiceStack.Text;
using ServiceStack.DataAnnotations;

public sealed class SomeObject
{
    [Alias("expand")]
    public string Expand { get; set; }

    [Alias("projects")]
    public List<string> Projects {get; set; }
}
Moo-Juice
  • 38,257
  • 10
  • 78
  • 128
  • Is this relevant to the OP? The question's about receiving different strings between WebClient,Chrome and IE. not JSON de/serialization. – Mesh Sep 13 '13 at 07:46
  • It *is* about JSON de/serialization (as far as I can tell!) The reason he sees fully formed results in Chrome is because Chrome *knows* it is JSON and is formatting the result appropriately. In his code, he just gets a string back. See the end of his question where he is assigning the result to a basic string. – Moo-Juice Sep 13 '13 at 07:49
  • It is my first time working with json, does it means that WebClient.DownloadString("") returns whole json data? does it menas that it gets json data but it cannot deserialze it? thanks – user217648 Sep 13 '13 at 07:56
  • @user217648, correct - `WebClient.DownloadString` will just return the raw, unformatted, string. To easily work with the data you need to de-serialize it. The ServiceStack.Text library has one of the most (if not *the* most) high-performance JSON serializing/deserializing. – Moo-Juice Sep 13 '13 at 07:57