Help with particular JSON

I have a json done this way:
-entry (title, image, image, image, image)
-entry (title, image, image, image, image)
-entry …

I want to show title and first image in a grid. How can I achieve this? If i define a DataSource with title and image fields, the image value showed on the grid is “imageValue1,imageValue2,imageValue3,imageValue4”, while I only want “imageValue1”.

Your JSON string should look like this one:

[
{"Value":"blabla","Image1":"pic1.png","Image2":"pic2.png"},
{"Value":"moreblabla on row2","Image1":"pic2.png","Image2":"pic3.png"}
]

Value, Image1, Image2 are your column / field names.

And I guess the images should contain some folder and maybe complete URL, but I have no experience with that yet.

I cannot modify the JSON, as it comes from a webservice.

You need to show the actual JSON you’re working with (a sample).

Here’s the actual JSON:

{
  "entry":[{
    "title":"titleA",
    "image":[{
       "url":"url0"
       },{
       "url":"url1"
    }]},
    {
    "title":"titleB",
    "image":[{
       "url":"url2"
       },{
       "url":"url3"
    }]
  }]
}

What I’m getting now is a grid with {“titleA”, “url0,url1”} in the first row and {“titleB”, “url2,url3”} in the second row because I’m using a Datasource with 2 fields:

  • one field with xPath = “title”
  • one field with xPath = “image/url”

Instead, I want a grid that shows {“titleA”, “url0”} in the first row and {“titleB”, “url2”} in the second row and so on if there are more entries.

As already pointed out, the JSON comes from a webservice, so I’m actually using a XJsonDataSource.

I hope now the problem is clear.

So basically, there are multiple URLs per entry but you only want to show the first. Use an XPath that ends in [0] for this, something like valueXPath:“image/url[0]” (note: haven’t tested this).

This is even worse: now it doesn’t get any value. I also tried image[0]/url, and it even get stuck on the “Getting data from server” window.
Can’t I modify the JSON before it is processed by the grid?
Or can’t I define another DataSource for the image, and then include the first url in the main DataSource? I’m just guessing here, I’m new to both Smart client and smartgwt.

Anyone can help here? I think that if this problem doesn’t have a solution, then that is a serious issue. Actually the JSON format provided in this thread is a semplification of the google api data feed, and not some exotic JSON API.
If there is no solution to this, I can always do this server side with php or java, but that is clearly the worst case, as it adds latency to the user request.
Thank you in advance.

This problem has many client-side solutions. If you can’t address it via valueXPath, transformResponse() lets you do anything you want to the data.

But the problem is most likely that the XPath is wrong. Read the docs for selectObjects and try to figure out the right XPath.

“Read the docs for selectObjects”
That’s exactly what I needed! :slight_smile: From the doc: “NOTE: in XPath, Array index starts at 1, not 0)”
So the right path was image/url[1].
Thanky you very much.