Burhan Mughal
Burhan Mughal

Reputation: 818

Json image parsing

Hi here is my Json link from where i want to parse the image in my application

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

The problem is this that while parsing it parse the text i.e. the title and nid but not the picture. Can any one help how to parse an object containing white space in it .. i.e. in my json there is an image named (Main image). this is the main problem. here is my code to parse Json

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

Thanks in Advance

Upvotes: 1

Views: 1880

Answers (2)

balexandre
balexandre

Reputation: 75083

Here's my new answer.

You will have a problem by using the JavaScriptSerializer as your incoming JSON is like this:

[
   {
      "node_title":"<a href=\"/item/brickell-point\">Brickell Point</a>",
      "main image":"<img typeof=\"foaf:Image\" src=\"http://collectionking.com/sites/default/files/styles/collection_list/public/BrickellPoint_front.jpg\" width=\"200\" height=\"250\" alt=\"\" />",
      "nid":"123"
   },
   {
      "node_title":"<a href=\"/item/flagler-street-bridge\">Flagler Street Bridge</a>",
      "main image":"<img typeof=\"foaf:Image\" src=\"http://collectionking.com/sites/default/files/styles/collection_list/public/FlaglerStreetBridge_Miami_River.Front_.jpg\" width=\"200\" height=\"250\" alt=\"\" />",
      "nid":"124"
   }
]

and there's the image property with a space in it... that's not accomplishable, so let's use JSON.NET for this.

Step 1 - Right click on References of your project and choose Manage Nuget Packages and in the "Online" tab, install JSON.NET

After that, use this code:

// grab information
using (var wc = new WebClient()) {
    json = wc.DownloadString(url);
}

// deserialize using JSON.NET
var result = Newtonsoft.Json.JsonConvert.DeserializeObject<ck[]>(json);

// output
foreach (var i in result)
{
    lblTitle.Text = i.node_title;
    lblNid.Text = i.nid;
    imgCk.ImageUrl = i.main_image;}
}

and remember to modify your ck object to

public class ck
{
    public string node_title { get; set; }
    public string nid { get; set; }

    [Newtonsoft.Json.JsonProperty(PropertyName = "main image")]
    public string main_image { get; set; }
}

where we use the JsonProperty to map the correct attribute that we are receiving.

To extract the title and image out of the tags, and cause I don't like RegEx, I use:

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);
}

So you can use it your self as:

// output
foreach (var i in result)
{
    // for this example, let's grab the last 
    lblTitle.Text= ExtractTitleFromTag(i.node_title);
    lblNid.Text= i.nid;
    imgCk.ImageUrl= ExtractImageFromTag(i.main_image);
}

Now... you do realize that your foreach will only get the last entry of the receiving JSON right? as you're not

you should simply do:

foreach (var i in result)
{
    i.node_title = ExtractTitleFromTag(i.node_title);
    i.main_image = ExtractImageFromTag(i.main_image);
}

and use the DataGrid to show more than a entry, for example.

myDataGrid.Source = ck;

Upvotes: 2

Codo
Codo

Reputation: 78845

The main_image attribute doesn't contain an image URL. Instead, it contains a piece of HTML, e.g.:

<img typeof="foaf:Image" src="http://collectionking.com/sites/default/files/styles/collection_list/public/FlaglerStreetBridge_Miami_River.Front_.jpg" width="200" height="250" alt="" />

You can use regular expressions to extract the URL (it's not fool proof, but might work in most cases):

string imageUrl = null;
string htmlPiece = i.main_image;
Match m = Regex.Match(htmlPiece, "src=\"[^\"]+\"");
if (m.Success)
  imageUrl = m.Value.Substring(5, m.Value.Length - 6);

Upvotes: 1

Related Questions