1

I have a jsonarray and for that i have created pojo class. Now, i want to set jsonarray into a string. When i am doing that it throws error of The JsonDeserializer StringTypeAdapter failed to deserialize json object given the type class java.lang.String . And this question is differ because i want to set that jsonarray into String So, any one have any idea ?

3
  • Could you incluse some of your code? Commented Sep 25, 2015 at 6:24
  • 3
    stackoverflow.com/questions/4586229/… Commented Sep 25, 2015 at 6:24
  • But, i want to set that jsonarray into a string variable. So, can i do that? Commented Sep 25, 2015 at 6:35

3 Answers 3

1

Gson.toJson() returns a String..

So like..

String myArrayAsAString = gsonInstance.toJson(myJsonArray);

Is that what you mean?

Sign up to request clarification or add additional context in comments.

1 Comment

Where i put this code? I mean i called this when my json came, Gson gson = new GsonBuilder().serializeNulls().create(); ProductInfo item = gson.fromJson(response.toString(), POJO_Class.class); and my POJO_Class have getter and setter methods.
1

u can set the method uself. public class BookDeserializer implements JsonDeserializer {

@Override
public Book deserialize(final JsonElement json, final Type typeOfT, final JsonDeserializationContext context)
  throws JsonParseException {
final JsonObject jsonObject = json.getAsJsonObject();

final JsonElement jsonTitle = jsonObject.get("title");
final String title = jsonTitle.getAsString();

final String isbn10 = jsonObject.get("isbn-10").getAsString();
final String isbn13 = jsonObject.get("isbn-13").getAsString();

final JsonArray jsonAuthorsArray = jsonObject.get("authors").getAsJsonArray();
final String[] authors = new String[jsonAuthorsArray.size()];
for (int i = 0; i < authors.length; i++) {
  final JsonElement jsonAuthor = jsonAuthorsArray.get(i);
  authors[i] = jsonAuthor.getAsString();
}

final Book book = new Book();
book.setTitle(title);
book.setIsbn10(isbn10);
book.setIsbn13(isbn13);
book.setAuthors(authors);
return book;
}
}    

http://www.javacreed.com/gson-deserialiser-example/

2 Comments

But for this i have to make separate classes, because my response is too long, and it have more than 4 array. So, for all i have to make classes ?
maybe u should provide the json ,the POJO class and the place u want to set that jsonarray into String
1

Say you have an array of productInfo:

List<ProductInfo> productInfos = new ArrayList<>();
// fill your productInfos, etc.

Then you want to get String representation of productInfos' JsonArray:

JsonArray productInfoJsonArray = (JsonArray) new Gson().toJsonTree(productInfos,
            new TypeToken<List<ProductInfo>>() {
            }.getType());

productInfoJsonArray.getAsString();

I'm not entirely sure about what you're trying to achieve, but I hope that above snippet helps.

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.