0

I have the following string

    String new_value=
   {
        product_type_id: 1,
        product_cat_id: 1,
        product_type_nm: PED
    }, {
        product_type_id: 2,
        product_cat_id: 2,
        product_type_nm: MOBILE APP
    }, {
        product_type_id: 3,
        product_cat_id: 1,
        product_type_nm: MOBILE
    }, {
        product_type_id: 4,
        product_cat_id: 3,
        product_type_nm: PAYMENT
    }, {
        product_type_id: 5,
        product_cat_id: 5,
        product_type_nm: USER
    }, {
        product_type_id: 6,
        product_cat_id: 6,
        product_type_nm: SMS
    }, {
        product_type_id: 9,
        product_cat_id: 6,
        product_type_nm: EMAIL
    }, {
        product_type_id: 10,
        product_cat_id: 6,
        product_type_nm: TOPUP
    }

This String contains data related to many beans. I want to convert it into json string and then into bean in java.How can i do it? I am using

JSONObject jsonObj = new JSONObject(new_value);

Is it possible with this.

Ramsad
  • 63
  • 1
  • 1
  • 11

2 Answers2

1

For that you can use use Jackson to convert Java object to / from JSON

ObjectMapper mapper = new ObjectMapper();
String jsonInString = "{'name' : 'vinit'}";
Staff obj = mapper.readValue(jsonInString, Staff.class); //String to Class Object

Before that you need to add jackson-databind lib.

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.6.3</version>
</dependency>

you can see more example on click here

Vinit Solanki
  • 1,863
  • 2
  • 15
  • 29
0

Look at the below link:

Generate Java class from JSON?

They are converting a Json to POJO. There are multiple tools available online and you can download the same once done.If there are any exceptions, share the trace logs and someone can help.

Community
  • 1
  • 1
Saurabh Jhunjhunwala
  • 2,832
  • 3
  • 29
  • 57