Good day people.
I have this JSON result here http://pastebin.com/9psYCfGj.
And I want to get the first 2 backdrops (Start at line 607).
So I want to get the URL of the first backdrop with the size of w1280 (line 639)
And the URL of the second backdrop with the size of w1280 (line 680)
How would I only get these two URLs
Thanks
Tom
The example JSON in the original question was over 800 lines. Here’s a trimmed version that retains the same structure (but is still longish at 50 lines).
As already suggested, a simple approach would be to use Gson to deserialize the JSON into a Java data structure, and then iterate through the Java structure to select the target data. Alternatively, there do exist at least two Java-to/from-JSON libraries that allow xpath-like selection queries to simply pull out just the target data, but they’re very slow compared to other Java libraries for reading JSON, and they don’t yet have a lot of community use and support, so I wouldn’t recommend them. At any rate, whether using Gson or any other Java-to/from-JSON library, you’d do well to decide on selection criteria other than line number.
Assuming that the selection goal includes the URLs of the first two backdrops with size w1280, following is an example of using Gson to accomplish it.
The situation is complicated a little in that the JSON structure wraps the single response object in an array, and wraps each of the image objects in an array. So, the following Java data structure simply matches this structure exactly.
Note: Since Poster and Backdrop have the same exact structure (they just contain an Image reference), they could easily be merged into a single data type.