1

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

1
  • can you post the service URL for that? Where did you get the data from? Jul 11, 2011 at 17:29

4 Answers 4

0

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).

[
    {
        "id": 17529,
        "name": "True Grit",
        "posters": [
            {
                "image": {
                    "type": "poster",
                    "size": "original",
                    "height": 1500,
                    "width": 1000,
                    "url": "http://cf1.imgobject.com/posters/0be/4db5f8e65e73d67a840070be/true-grit-original.jpg",
                    "id": "4db5f8e65e73d67a840070be"
                }
            },
            {
                "image": {
                    "type": "poster",
                    "size": "w154",
                    "height": 211,
                    "width": 154,
                    "url": "http://cf1.imgobject.com/posters/284/4d559bb87b9aa15cf6001284/true-grit-w154.jpg",
                    "id": "4d559bb87b9aa15cf6001284"
                }
            }
        ],
        "backdrops": [
            {
                "image": {
                    "type": "backdrop",
                    "size": "w1280",
                    "height": 720,
                    "width": 1280,
                    "url": "http://cf1.imgobject.com/backdrops/900/4d33ccea7b9aa177db007900/true-grit-w1280.jpg",
                    "id": "4d33ccea7b9aa177db007900"
                }
            },
            {
                "image": {
                    "type": "backdrop",
                    "size": "w1280",
                    "height": 720,
                    "width": 1280,
                    "url": "http://cf1.imgobject.com/backdrops/01f/4c90088b5e73d61ee900001f/true-grit-w1280.jpg",
                    "id": "4c90088b5e73d61ee900001f"
                }
            }
        ]
    }
]

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.

import java.io.FileReader;
import java.lang.reflect.Type;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class Foo
{
  public static void main(String[] args) throws Exception
  {
    Gson gson = new Gson();
    Type movieListType = new TypeToken<List<Movie>>() {}.getType();
    List<Movie> movies = gson.fromJson(new FileReader("input.json"), movieListType);

    // movies has just one Movie
    Movie movie = movies.get(0);

    // Find the first two Backdrop URLs with size = w1280
    int foundCount = 0;
    for (Backdrop backdrop : movie.backdrops)
    {
      if ("w1280".equals(backdrop.image.size))
      {
        System.out.println("URL Found: " + backdrop.image.url);
        foundCount++;
        if (foundCount == 2)
        {
          break;
        }
      }
    }
  }
}

class Movie
{
  int id;
  String name;
  List<Poster> posters;
  List<Backdrop> backdrops;
}

class Poster
{
  Image image;
}

class Backdrop
{
  Image image;
}

class Image
{
  String type;
  String size;
  int height;
  int width;
  String url;
  String id;
}

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.

0

Well, you will have to parse the string using a JSON parser. That's the easiest. But directly getting those 2 URLs is not possible by using a JSON parser. Also, you will need to have a criterion based on which you will pick up those two URLs while parsing the string.

You can't access it by line numbers.

0

Using GSON. You can define classes

class Poster {
 static class Image {
    String url;
 }
 Image image;
}
class Result {
 List<Poster> posters;
}

Then using GSON api you can deserialize it. But be aware that GSON will create list with all elements so it will eat you memmory. Another way is the use of regular expression but it could take much more memmory. I think the most efficient way will be writing own loop-based logic :/

1
  • The number of items in the two JSON lists are relatively few, so I wouldn't be concerned about excessive memory consumption. Jul 11, 2011 at 19:19
0
JSONArray allData;
JSONObject individualMovie;
JSONArray backDrops;
JSONObject single_backdrop;

allData = someMethodToGetJsonFromUrl;

int movieCount = 0;
while(!allData.isNull(movieCount))
{
    individualMovie = allData.optJSONObject(showCount);
    backDrops = individualMovie.optJSONArray("backdrops");
    //WE now have all the backdrops to movie0
    //to get the data from the FIRST backdrop:

    single_backdrop = backDrops.optJSONObject(0).optJSONbject("image");

    //NOW  we can access the id/url/height/width
    String URL = single_backdrop.optString("url");

    //do something with url
    movieCount++;
    //loop through all movies
}

this is a little rough but I hope you get the idea

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.