Java String to JSON conversion

后端 未结 4 937
遇见更好的自我
遇见更好的自我 2021-01-01 16:23

i am getting data from restful api in String variable now i want to convert to JSON object but i am having problem while conversion it throws exception .Here is my code :

4条回答
  •  既然无缘
    2021-01-01 17:11

    The name is present inside the data. You need to parse a JSON hierarchically to be able to fetch the data properly.

    JSONObject jObject  = new JSONObject(output); // json
    JSONObject data = jObject.getJSONObject("data"); // get data object
    String projectname = data.getString("name"); // get the name from data.
    

    Note: This example uses the org.json.JSONObject class and not org.json.simple.JSONObject.


    As "Matthew" mentioned in the comments that he is using org.json.simple.JSONObject, I'm adding my comment details in the answer.

    Try to use the org.json.JSONObject instead. But then if you can't change your JSON library, you can refer to this example which uses the same library as yours and check the how to read a json part from it.

    Sample from the link provided:

    JSONObject jsonObject = (JSONObject) obj;
    String name = (String) jsonObject.get("name");
    

提交回复
热议问题