1

I convert JSONObject in string for parse it in JsonNode with jackson but i have a List in my JSONObject and when i parse it with a ObjectMapper i get this :

["{Property1 : value1, Property2 : value2}"]

And i can't call myJsonNodeObject.get(i).get("Property1") this is my problem.

I have tried to cast my List in JSONArray in my JSONObject but don't work.

resultAsJSONObject = new JSONObject();
resultAsJSONObject.put("Label", getMetricStatisticsResult.getLabel());
resultAsJSONObject.put("Datapoints", getMetricStatisticsResult.getDatapoints());
resultAsJSONObject.put("fromDate", fromDate.getTimeInMillis());
resultAsJSONObject.put("toDate", toDate.getTimeInMillis());
resultAsJSONObject.put("error", "");
resultAsString = resultAsJSONObject.toString();

mapper.readValue(resultAsString, MetricsData.class);
8
  • 1
    Can you please share your MetricsData bean and how you use it? Commented Apr 20, 2015 at 10:51
  • Why ? You don't need it. The problem is the List in JSONObject (getMetricsStatisticsResult.getDatapoints() return the List), it is convert to String with this format ["{Property1 : value1, Property2 : value2}"] but it's wrong i need this format : [{"Property1" : "value1", "Property2" : "value2"}"]. Commented Apr 20, 2015 at 14:39
  • I wonder, why are you using JSONObject ? Can you just drop it? Commented Apr 20, 2015 at 21:04
  • Can i do this otherwise ? I use JSONObject because i just know this solution. What do you want i drop ? Commented Apr 21, 2015 at 7:21
  • So i have add <code>resultAsString = resultAsString.replace("\"[", "["); resultAsString = resultAsString.replace("]\"", "]"); resultAsString = resultAsString.replace("\\\"", "\""); </code> ; but i think it's bad solution. Commented Apr 21, 2015 at 7:42

2 Answers 2

3

Assuming that you have a JSON string which you just want to change. Then you can use Jackson to parse it as a ObjecNode and then modify it. Here is an example:

public class JacksonModifyJson {

    static final String JSON = "{\"name\":\"Bob\", \"age\":13}";

    public static void main(String[] args) throws IOException {
        final ObjectMapper mapper = new ObjectMapper();
        final ObjectNode jsonNode = mapper.readValue(JSON, ObjectNode.class);
        jsonNode.put("url", "example.com");
        System.out.println(mapper.writeValueAsString(jsonNode));
    }
}

Output:

{"name":"Bob","age":13,"url":"example.com"}
Sign up to request clarification or add additional context in comments.

Comments

0

THis method is really easy and works too

try {

    JSONObject jsonObject = new JSONObject(THESTRINGHERE);
    String[] names = JSONObject.getNames(jsonObject);
    JSONArray jsonArray = jsonObject.toJSONArray(new JSONArray(names));

    ArrayList<String> listdata = new ArrayList<String>();     
    JSONArray jArray = (JSONArray)jsonArray; 
    if (jArray != null) { 
       for (int i=0;i<jArray.length();i++){ 
        listdata.add(jArray.get(i).toString());
       } 
    } 
//  System.out.println(listdata);



} catch (Exception e) {
    System.out.println(e.getMessage());
}

1 Comment

This gives an error in new JsonObject (The string here) that cannot convert string into System.Collection.Generic.IEnumerable.

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.