내 커뮤니티 업적 표시줄을 확대합니다.

Submissions are now open for the 2026 Adobe Experience Maker Awards.

Mark Solution

활동이 없어 이 대화는 잠겼습니다. 새 게시물을 작성해 주세요.

해결됨

convert stringified array of objects to json object

Avatar

Level 5

hi this is more of a java based question but I could use some help here-

 

i have a stringified (json.stringify) javascript array of strings ["abc","efg","ijk"]

i want to convert it to a json object

 

JsonObject jsonObject = new Gson().fromJson(arrayOfStrings, JsonObject.class);
com.google.gson.JsonSyntaxException: Expected a com.google.gson.JsonObject but was com.google.gson.JsonArray

 

i have tried using JsonParser from gson, javax.json.stream but nothing seems to work.

주제

토픽은 커뮤니티 콘텐츠를 분류하여 관련성 있는 콘텐츠를 찾는 데 도움이 됩니다.

1 채택된 해결책 개

Avatar

정확한 답변 작성자:
Community Advisor

If it is only an array of string you want, you can use the below - 

List<String> currentGroupIdsList = new ArrayList<String>(Arrays.asList(stringifiedArray.split(",")));

Here stringifiedArray is received from the frontend and converted into simple <List> of strings.

Hope this helps! 

원본 게시물의 솔루션 보기

10 답변 개

Avatar

Community Advisor

Try .fromJson(arrayOfStrings, JsonArray,class)  instead of converting it into JsonObject.class


Avatar

Level 5

i tried into jsonarray too but that didn't work

Avatar

Level 4

To fix this, you should use the appropriate data structure for deserializing the JSON array. In this case, you should use JsonArray instead of JsonObject since the input is an array of strings.

 

 

String arrayOfStrings = "[\"abc\",\"efg\",\"ijk\"]";

// Parse the JSON array into a JsonArray
JsonArray jsonArray = new Gson().fromJson(arrayOfStrings, JsonArray.class);

// Now you can work with the JsonArray as needed
sout(jsonArray); // Output: ["abc","efg","ijk"]

 

 

JsonArray object is ready here to access the individual elements within the array if needed

Avatar

Level 5

I have tried json array too but that throws an error too.

Avatar

Employee Advisor

Hi @aem_noob ,

 

I don't know what a full string looks like, but this seems to be JsonArray, try using that class instead.

 

JsonArray jsonArray =  new Gson().toJsonTree(arrayOfStrings).getAsJsonArray();

You could then iterate and read the JsonObject in this array, or form the proper JsonString which would qualify as JsonObject.

Also, check https://www.tutorialspoint.com/how-to-convert-java-array-or-arraylist-to-jsonarray-using-gson-in-jav... 

 

Hope this helps.

 

Regards,

Nitesh

 

Avatar

Level 5

i tried jsonarray initially but that also threw an exception. the response from front end is a json of array objects which is then filtered to array of strings. the array of strings is not getting cast to jsonarray.

Avatar

Community Advisor

Hi @aem_noob 

 

Try this ->

 

const array = ["abc", "efg", "ijk"];

 

// Convert array to JSON object const jsonObject = JSON.stringify(array);

 

console.log(jsonObject);

Avatar

Level 5

The array of strings is not getting cast to jsonObject as I have tried it

Avatar

정확한 답변 작성자:
Community Advisor

If it is only an array of string you want, you can use the below - 

List<String> currentGroupIdsList = new ArrayList<String>(Arrays.asList(stringifiedArray.split(",")));

Here stringifiedArray is received from the frontend and converted into simple <List> of strings.

Hope this helps! 

Avatar

Level 5

this works but isn't it possible to do it via jsonArray or jsonObject?