14598

Issue in Parsing Json image in C#

Question:

net C#. I am trying to parse Json from a webservice. I have done it with text but having a problem with parsing image. Here is the Url from where I m getting Json

http://collectionking.com/rest/view/items_in_collection.json?args=122

And this is My code to Parse it

using (var wc = new WebClient()) { JavaScriptSerializer js = new JavaScriptSerializer(); var result = js.Deserialize<ck[]>(wc.DownloadString("http://collectionking.com/rest/view/items_in_collection.json args=122")); foreach (var i in result) { lblTitle.Text = i.node_title; imgCk.ImageUrl = i.["main image"]; lblNid.Text = i.nid;

Any help would be great. Thanks in advance. PS: It returns the Title and Nid but not the Image. My class is as follows:

public class ck { public string node_title; public string main_image; public string nid; }

Answer1:

Try This

private static string ExtractImageFromTag(string tag) { int start = tag.IndexOf("src=\""), end = tag.IndexOf("\"", start + 6); return tag.Substring(start + 5, end - start - 5); } private static string ExtractTitleFromTag(string tag) { int start = tag.IndexOf(">"), end = tag.IndexOf("<", start + 1); return tag.Substring(start + 1, end - start - 1); }

It may help

Answer2:

Your problem is that you are setting ImageUrl to something like this <img typeof="foaf:Image" src="http://... and not an actual url. You will need to further parse main image and extract the url to show it correctly.

<strong>Edit</strong>

This was a though nut to crack because of the whitespace. The only solution I could find was to remove the whitespace before parsing the string. It's not a very nice solution but I couldn't find any other way using the built in classes. You might be able to solve it properly using <a href="http://json.net" rel="nofollow">JSON.Net</a> or some other library though.

I also added a regular expression to extract the url for you, though there is no error checking what so ever here so you'll need to add that yourself.

using (var wc = new WebClient()) { JavaScriptSerializer js = new JavaScriptSerializer(); var result = js.Deserialize<ck[]>(wc.DownloadString("http://collectionking.com/rest/view/items_in_collection.json?args=122").Replace("\"main image\":", "\"main_image\":")); // Replace the name "main image" with "main_image" to deserialize it properly, also fixed missing ? in url foreach (var i in result) { lblTitle.Text = i.node_title; string realImageUrl = Regex.Match(i.main_image, @"src=""(.*?)""").Groups[1].Value; // Extract the value of the src-attribute to get the actual url, will throw an exception if there isn't a src-attribute imgCk.ImageUrl = realImageUrl; lblNid.Text = i.nid; } }

Recommend

  • How to reformulate an sql query
  • Regex Notepad++ find & replace 2nd set of characters
  • Deserialize JSON without key
  • Tomcat 6 thread issue
  • Using WebClient to ping a web site
  • Detect site login url in Application_End
  • OpenGL to FFMpeg encode
  • The length of the string exceeds the value set on the maxJsonLength property.c#
  • Wordpress Mobile - Change Part of the URL Instead of Redirecting to the Front Page?
  • Task getting rejected although threads are available
  • How would i implement an infinite scroll in my DOM
  • How to map clock gate to tech library cell
  • How to resolve “ Multiplicity is not valid in Role” error?
  • Escaping single quotes in JDBC with MySql
  • How to stamp out template in self contained custom elements with vanilla js?
  • Programmatically access files in Document set in sharepoint using Javascript
  • How can I sort a a table with VBA with given text condition?
  • How to Cache Real-time Data?
  • Abort upload large uploads after reading headers
  • JSON response opens as a file, but I can't access it with JavaScript
  • Play WS (2.2.1): post/put large request
  • Set the selected item in dropdownlist in MVC3
  • Python CGI os.system causing malformed header
  • DomPDF {PAGE_NUM} not on first page
  • AES padding and writing the ciphertext to a disk file
  • How to convert from System.Drawing.Color to Excel.ColorFormat in C#? Change comment color
  • Why doesn't :active or :focus work on text links in webkit? (safari & chrome)
  • output of program is not same as passed argument
  • Validaiting emails with Net.Mail MailAddress
  • MySQL WHERE-condition in procedure ignored
  • Web-crawler for facebook in python
  • Akka Routing: Reply's send to router ends up as dead letters
  • trying to dynamically update Highchart column chart but series undefined
  • unknown Exception android
  • Checking variable from a different class in C#
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal