1

I am trying to covert the following JSON which contains an object of json objects into a Java object on Android using gson.fromJson(json, packageClass), however, I am having trouble figuring out what the structure of my java package class should be.

{
"package": {
    "version": "1.0.0",
    "description": "my test description",
    "contents": {
        "product1": {
            "description": "my product 1"
        },
        "product 2": {
            "description": "my product 2"
        },
        "product 3": {
            "description": "my product 3"
        }
    }
}

}

2 Answers 2

3

One option is to create a class where the field names match the name of the JSON fields:

public class Entry {
  Map<String, Object> package; 
}

This is very basic and minimal.

Another option is to create the classes:

public class Entry {
  Package package; 
}

public class Package {
  String version;
  String description:
  Map<String, Product> contents; 
}


public class Product {
  String description:
}
Sign up to request clarification or add additional context in comments.

Comments

1

use gson.fromJson to convert json string into bean class object

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.