Click here to Skip to main content
15,884,177 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm trying to read from a http address and from there parse in a Json string.
It says it cannot convert from system.IO.Stream to string.

Its the "(Json)" in "List<collectiondataweeks> weeks = Newtonsoft.Json.JsonConvert.DeserializeObject<list><collectiondataweeks>>(json);" it doesn't recognize as a string. What am I missing?
public class collectionDataWeeks
    public string week { get; set; }
    public string mothertext { get; set; }
    public string babytext { get; set; }

private void myButton1_Click(object sender, RoutedEventArgs e)

    string weekNumber = "2";
    string uriHost = "http://.....php?id=";
    string uriGet = uriHost + weekNumber;

    var client = new WebClient();
    client.OpenReadCompleted += new OpenReadCompletedEventHandler(returnJson);
    client.OpenReadAsync(new Uri(uriGet));


private void returnJson (object sender, OpenReadCompletedEventArgs e)

    var json=e.Result;

    List<collectiondataweeks> weeks = Newtonsoft.Json.JsonConvert.DeserializeObject<list><collectiondataweeks>>(json);

    string uge = "2";
    var varWeeks = weeks.Where(x => x.week == uge);

    foreach (collectionDataWeeks itm in varWeeks)
        textBoxWeek0.Text = itm.week;
Updated 9-Nov-12 6:04am
Sergey Alexandrovich Kryukov 9-Nov-12 12:23pm    
Kasper Andersen 9-Nov-12 12:34pm    
I need to retrieve some data and its not possible for me to use wcf.
Only Json data strings.

How about using System.Runtime.Serialization.Json.DataContractJsonSerializer instead of using the 3rd-party stuff? Please see:[^],[^].

Share this answer
Kasper Andersen 9-Nov-12 12:40pm    
My knowledge is not very good about any of them. But if you would show me how my code would work with it. I would use it. Unfortunately I'm not advanced enough to understand and use methods directely out of msdn. I need to see examples that resemble mine to understand it.
Sergey Alexandrovich Kryukov 9-Nov-12 13:17pm    
If you cannot read and understand MSDN help, with code samples and detailed explanation, I cannot see a whole lot of sense in helping you, sorry. Even though I already provided help in more clear way that can be found in MSDN, I cannot guarantee it in all cases. Besides, I don't see a compelling reason for downloading of Newtonsoft Json.NET...
Kasper Andersen 9-Nov-12 17:58pm    
I would really like a solution instead of a link to msdn or a lecture of other methods to use without examples but if you dont see why, im sorry. I just wanted some help.
Also if my code is so bad that it needs an overhaul I will understand.
Kasper Andersen 10-Nov-12 4:22am    
Fair enough but I think you miss understood. The problem was never with the serializer. It works fine. It was the code that would retrieve the string from the URL that doesn't work.
Sergey Alexandrovich Kryukov 10-Nov-12 20:33pm    
Oh... Sorry about that; this is my fault: I really misunderstood. Let me see; this should be something simple enough...
I forgot to do streamreader!!


private void returnJson(object sender, OpenReadCompletedEventArgs e)
            Stream stream = e.Result;
            StreamReader itmStreamReader = new StreamReader(stream);
            string json = itmStreamReader.ReadToEnd();
            List<collectiondataweeks> weeks = Newtonsoft.Json.JsonConvert.DeserializeObject<list><collectiondataweeks>>(json);


            string uge = "2";

Now everything works and the Json is returned from the URL as a string.

[edit]code block added[/edit]
Share this answer
Sergey Alexandrovich Kryukov 10-Nov-12 21:18pm    
No, it cannot work exactly because it does not return anything. You need to return weeks, or something else. As is, json, weeks and uge are obtained, but after return, these local (stack) values a lost (unreachable) and will be garbage-collected eventually.

Besides, StreamReader is used incorrectly; there is not catch-try. It should be done with "using" statement and IDisposable (see):
using (StreamReader reader = new StreamReader(/* ... */)) {
// use reader
} // at this point reader.Dispose is called; not need to close

If you fix it, it may work. Also, you code should give your warning. Never work with unresolved warning...
If you mind, you can check an article I wrote about serialization, source code is included, you can always find out what's missing.

Switching Seamlessly Between Serialization[^]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900