Click here to Skip to main content
12,349,148 members (63,700 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# VS2010 JSON
Hi

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;
    }
}
Posted 9-Nov-12 5:53am
Edited 9-Nov-12 6:04am
v3
Comments
   
Why?
--SA
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.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

How about using System.Runtime.Serialization.Json.DataContractJsonSerializer instead of using the 3rd-party stuff? Please see:
http://msdn.microsoft.com/en-us/library/system.runtime.serialization.json.datacontractjsonserializer.aspx[^],
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

—SA
  Permalink  
Comments
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.
   
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...
--SA
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...
--SA
Sergey Alexandrovich Kryukov 10-Nov-12 21:20pm
   
Well, you almost got it, but this is not a solution yes. I commented on what you have to fix...
--SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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);
 
            stream.Close();
            itmStreamReader.Close();
 
            string uge = "2";
        }
...

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

[edit]code block added[/edit]
  Permalink  
v2
Comments
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...
--SA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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[^]
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160621.1 | Last Updated 10 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100