0

I am using an api to get data for my application. I parse the JSON data using GSON so I have something looing like this:

List
    obj
        double
        movie
            ...
    obj
        double
        movie
            ...
    ...

The double value is used for weighting the order of the movies. However, I am not intrested in that, and would instead look a plain list of movies, like this:

List
    movie
        ...
    movie
        ...
    ...

The only solution I can think of is first parsing it to a list of obj and then using a loop to copy the movie objects to a new list, but I was hoping you could teach me a better method. What are your suggestions?

user3600338
  • 119
  • 1
  • 10
  • May be this will help: http://stackoverflow.com/questions/4802887/gson-how-to-exclude-specific-fields-from-serialization-without-annotations – Sanj Mar 09 '16 at 17:56

2 Answers2

0

I seem to recall that giving your field the transient keyword should do the trick:

private transient double value;

This should exclude it from serialization.

PNS
  • 750
  • 1
  • 5
  • 19
0

You can use JsonPath library, along with JSONExpression to extract the required element(s) from JSON. E.g. in case of below json:

[{
    "test": 1,
    "test1": "value1"
},
{
    "test": 1,
    "test1": "value2"
}]

If you want to get all the values of test1 element then, you can use $[*].test1 JSONExpression to get both value1 and value2. See the java example below:

public static void main(String[] args) {
    Object value = JsonPath.read("[{    \"test\": 1,    \"test1\": \"value1\"},{    \"test\": 1,    \"test1\": \"value2\"}]", "$[*].test1");
    System.out.println(value);
}

This is a good website to test JSON expressions on the fly.

*edit* Maven dependency for JsonPath :

<dependency>
    <groupId>com.jayway.jsonpath</groupId>
    <artifactId>json-path</artifactId>
    <version>2.0.0</version>
</dependency>
Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102