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");

9
  • 2
    get the json.net library to serialize / deserialize any json you encounter. Commented Sep 13, 2013 at 7: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. Commented Sep 13, 2013 at 7:40
  • @Adrian, I don't see what authentication has to do with the OP's question? Commented Sep 13, 2013 at 7: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. Commented Sep 13, 2013 at 7:48
  • @user217648 What string do you get back in each case? Commented Sep 13, 2013 at 7:55

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

2 Comments

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.
I don't know much about JIRA but some searching lead me here stackoverflow.com/questions/11869780/… Also Atalssian/JIRA documentation seems very thorough developer.atlassian.com/display/JIRADEV/…
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; }
}

4 Comments

Is this relevant to the OP? The question's about receiving different strings between WebClient,Chrome and IE. not JSON de/serialization.
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.
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, 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.