Can we convert a string to JSON Array using the json-simple-1.1.1.jar library?

前端 未结 1 746
日久生厌
日久生厌 2021-01-24 04:25

I want to convert a string into a JSON Array using the json-simple-1.1.1.jar library and came up with the following code,

import org.json.simple.*;
         


        
相关标签:
1条回答
  • 2021-01-24 04:50

    What am I doing wrong here?

    You're trying to convert a String that contains a JSON array into a JSONObject

    JSONObject jsonObject = new JSONObject(output);
    

    Your content represents a JSON array so parse it as such

    JSONParser parser = new JSONParser();
    JSONArray jsonArray = (JSONArray) parser.parse(output);
    

    Note that other libraries, like Gson and Jackson, have much better abstractions for JSON arrays and objects (JsonArray, ArrayNode). Consider using those instead.

    0 讨论(0)
提交回复
热议问题