C
C#3y ago
Steff

Stupid JSON Question

Good Morning. I want
"news": [
{
"sophoraId": "ukraine-annexion-kritik-101",
"news": [
{
"sophoraId": "ukraine-annexion-kritik-101",
alone in a richtextbox. When I try this now, the richtextbox is always empty :
richTextBox1.Text = "Titel : " + news.sophoraId;
richTextBox1.Text = "Titel : " + news.sophoraId;
13 Replies
Thinker
Thinker3y ago
How are you deserializing the JSON?
Steff
SteffOP3y ago
string stringResult = HttpWebRequest("GET", uri);
news = JsonConvert.DeserializeObject<News>(stringResult);
richTextBox1.Text = "Titel : " + news.sophoraId;
string stringResult = HttpWebRequest("GET", uri);
news = JsonConvert.DeserializeObject<News>(stringResult);
richTextBox1.Text = "Titel : " + news.sophoraId;
public static string HttpWebRequest(string method, string url)
{
WebRequest request = WebRequest.Create(url);
request.Method = method;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string streamResponses = string.Empty;
using (Stream dataStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(dataStream);

streamResponses = reader.ReadToEnd();
}
response.Close();
return streamResponses;
}
public static string HttpWebRequest(string method, string url)
{
WebRequest request = WebRequest.Create(url);
request.Method = method;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string streamResponses = string.Empty;
using (Stream dataStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(dataStream);

streamResponses = reader.ReadToEnd();
}
response.Close();
return streamResponses;
}
Thinker
Thinker3y ago
Firstly, don't use HttpWebRequest Use HttpClient
Steff
SteffOP3y ago
oh, it's old?
Thinker
Thinker3y ago
yes And not asynchronous
Steff
SteffOP3y ago
but HttpWebRequest is my Method Name
Thinker
Thinker3y ago
oof, I meant WebRequest anyway Looks like you're trying to serialize JSON which has an array as its root element into a non-array object?
Steff
SteffOP3y ago
wait, i have a idea This is the converted JSON in C# (incomplete) I need to use RootObject rather than the News class
public class Rootobject
{
public News[] news { get; set; }
public object[] regional { get; set; }
public string newStoriesCountLink { get; set; }
public string type { get; set; }
public string nextPage { get; set; }
}

public class News
{
public string sophoraId { get; set; }
public string externalId { get; set; }
public string title { get; set; }
public Teaserimage teaserImage { get; set; }
public DateTime date { get; set; }
public Tracking[] tracking { get; set; }
public Tag[] tags { get; set; }
public string updateCheckUrl { get; set; }
public int regionId { get; set; }
public string details { get; set; }
public string detailsweb { get; set; }
public string shareURL { get; set; }
public string topline { get; set; }
public string firstSentence { get; set; }
public Geotag[] geotags { get; set; }
public string ressort { get; set; }
public string type { get; set; }
}
public class Rootobject
{
public News[] news { get; set; }
public object[] regional { get; set; }
public string newStoriesCountLink { get; set; }
public string type { get; set; }
public string nextPage { get; set; }
}

public class News
{
public string sophoraId { get; set; }
public string externalId { get; set; }
public string title { get; set; }
public Teaserimage teaserImage { get; set; }
public DateTime date { get; set; }
public Tracking[] tracking { get; set; }
public Tag[] tags { get; set; }
public string updateCheckUrl { get; set; }
public int regionId { get; set; }
public string details { get; set; }
public string detailsweb { get; set; }
public string shareURL { get; set; }
public string topline { get; set; }
public string firstSentence { get; set; }
public Geotag[] geotags { get; set; }
public string ressort { get; set; }
public string type { get; set; }
}
Thinker
Thinker3y ago
Looks like it, yep
Steff
SteffOP3y ago
richTextBox1.Text = rootObject.news.ToString();
richTextBox1.Text = rootObject.news.ToString();
This comes out because of the toString() or not?
Chiyoko_S
Chiyoko_S3y ago
well, it's an array array doesn't have the ToString method overridden for display purposes you need to turn it into a string yourself
Thinker
Thinker3y ago
string.Join(", ", news) or something
Chiyoko_S
Chiyoko_S3y ago
well, News does not have a ToString defined grinowo could override the ToString I guess
Want results from more Discord servers?
Add your server