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");
-
2get the json.net library to serialize / deserialize any json you encounter.Wim Ombelets– Wim Ombelets2013-09-13 07:40:21 +00:00Commented 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.Mesh– Mesh2013-09-13 07:40:57 +00:00Commented Sep 13, 2013 at 7:40
-
@Adrian, I don't see what authentication has to do with the OP's question?Moo-Juice– Moo-Juice2013-09-13 07:43:02 +00:00Commented 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.Mesh– Mesh2013-09-13 07:48:28 +00:00Commented Sep 13, 2013 at 7:48
-
@user217648 What string do you get back in each case?Mesh– Mesh2013-09-13 07:55:42 +00:00Commented Sep 13, 2013 at 7:55
|
Show 4 more comments
2 Answers
You will need to authenticate the request via WebClient.
See this answer for how to do that if the site uses Forms Authentication.
2 Comments
user217648
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.
Mesh
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/…
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
Mesh
Is this relevant to the OP? The question's about receiving different strings between WebClient,Chrome and IE. not JSON de/serialization.
Moo-Juice
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.
user217648
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
Moo-Juice
@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.