Google gson.toJson(List) returning response as string instead of array

ravi nair

I am trying to use JsonObject to convert the java object to String. Following is the code that i am using to add the properties :

    JsonObject jsonObject = new JsonObject();
    jsonObject.addProperty("id", favoriteWrapper.getId());
    jsonObject.addProperty("menuitemid", favoriteWrapper.getMenuItemId());
    jsonObject.addProperty("displayname", favoriteWrapper.getDisplayName());
    jsonObject.addProperty("description", favoriteWrapper.getDescription());
    jsonObject.addProperty("alias", favoriteWrapper.getAlias());
    Gson gson = new Gson();
    jsonObject.addProperty("condiments", gson.toJson(favoriteWrapper.getCondiments()));

Here the last property condiments is a list of Long values and following is the response retrieved:

[
    {
        "id": 1,
        "menuitemid": 1,
        "displayname": "Ham",
        "description": "Ham",
        "alias": "Ham",
        "condiments": "[1,8,34,2,6]"
    }
]

Expected output is as following which is different for condiments:

   [
        {
            "id": 1,
            "menuitemid": 1,
            "displayname": "Ham",
            "description": "Ham",
            "alias": "Ham",
            "condiments": [1,8,34,2,6]
        }
    ]

What should I do to get the condiments as JSON array rather than String ?

ravi nair

I found the answer to my problem. I used JsonArray and JsonPrimitive to achieve the required response:

      JsonObject jsonObject = new JsonObject();
      jsonObject.addProperty("id", favoriteWrapper.getId());
      jsonObject.addProperty("menuitemid", favoriteWrapper.getMenuItemId());
      jsonObject.addProperty("displayname", favoriteWrapper.getDisplayName());
      jsonObject.addProperty("description", favoriteWrapper.getDescription());
      jsonObject.addProperty("alias", favoriteWrapper.getAlias());

      JsonArray condiments = new JsonArray();
         for (Long condimentId : favoriteWrapper.getCondiments()) {
                condiments.add(new JsonPrimitive(condimentId));
         }

      jsonObject.add("condiments", condiments);
      jsonObjects.add(jsonObject);

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Returning 'Range' instead of actual string in Google Apps Script

분류에서Dev

JSON response returning null array

분류에서Dev

NSArray Returning String Instead of Dictionary

분류에서Dev

json_decode is returning a null value instead of an array

분류에서Dev

Ruby String variable returning true for .is_a?(Array)

분류에서Dev

Returning array in Javascript function after splitting string

분류에서Dev

$_POST is returning just 'Array' as string and i am posting an Array

분류에서Dev

NSMutableArray to NSString conversion provides array instead of string

분류에서Dev

Wcf returning ususual response

분류에서Dev

php-google-spreadsheet-client getSpreadsheets returning empty array

분류에서Dev

Returning a PHP response in a AJAX query

분류에서Dev

Parsing JSON with google-gson

분류에서Dev

Gson custom deserializer for String class

분류에서Dev

Returning string is not successful

분류에서Dev

Function returning pointer to string

분류에서Dev

Returning an integer array

분류에서Dev

Returning Cython array

분류에서Dev

Angular provider returning an array

분류에서Dev

Returning an error response in the case of a null model

분류에서Dev

Laravel 4.2 returning a json response with Auth::onceBasic()

분류에서Dev

c function returning nan instead of double

분류에서Dev

SelectedItem.Text returning selectedvalue instead

분류에서Dev

Weird issue with returning empty struct instead of NULL

분류에서Dev

MySQL returning the MIN id instead of the MAX id?

분류에서Dev

OutOfMemoryError when using Gson to parse a large JSON response

분류에서Dev

[Android] Google Gson 충돌 앱

분류에서Dev

Gson name array and include a new property

분류에서Dev

Django views returning only one particular field instead of entire model

분류에서Dev

MySQL Query returning all dates instead of specified year and month clauses

Related 관련 기사

  1. 1

    Returning 'Range' instead of actual string in Google Apps Script

  2. 2

    JSON response returning null array

  3. 3

    NSArray Returning String Instead of Dictionary

  4. 4

    json_decode is returning a null value instead of an array

  5. 5

    Ruby String variable returning true for .is_a?(Array)

  6. 6

    Returning array in Javascript function after splitting string

  7. 7

    $_POST is returning just 'Array' as string and i am posting an Array

  8. 8

    NSMutableArray to NSString conversion provides array instead of string

  9. 9

    Wcf returning ususual response

  10. 10

    php-google-spreadsheet-client getSpreadsheets returning empty array

  11. 11

    Returning a PHP response in a AJAX query

  12. 12

    Parsing JSON with google-gson

  13. 13

    Gson custom deserializer for String class

  14. 14

    Returning string is not successful

  15. 15

    Function returning pointer to string

  16. 16

    Returning an integer array

  17. 17

    Returning Cython array

  18. 18

    Angular provider returning an array

  19. 19

    Returning an error response in the case of a null model

  20. 20

    Laravel 4.2 returning a json response with Auth::onceBasic()

  21. 21

    c function returning nan instead of double

  22. 22

    SelectedItem.Text returning selectedvalue instead

  23. 23

    Weird issue with returning empty struct instead of NULL

  24. 24

    MySQL returning the MIN id instead of the MAX id?

  25. 25

    OutOfMemoryError when using Gson to parse a large JSON response

  26. 26

    [Android] Google Gson 충돌 앱

  27. 27

    Gson name array and include a new property

  28. 28

    Django views returning only one particular field instead of entire model

  29. 29

    MySQL Query returning all dates instead of specified year and month clauses

뜨겁다태그

보관