Skip to content

Instantly share code, notes, and snippets.

@artur-kovalchuk
Last active February 6, 2020 20:56
Show Gist options
  • Select an option

  • Save artur-kovalchuk/22fdbdf2530ee4f9eea08068b8c9c9dd to your computer and use it in GitHub Desktop.

Select an option

Save artur-kovalchuk/22fdbdf2530ee4f9eea08068b8c9c9dd to your computer and use it in GitHub Desktop.
json
var obj = {
key1: "val1",
key2: "value2"
};
var json = JSON.stringify(obj);
----------------------------------------------------------
"{"key1":"val1","key2":"value2"}"
===
{
"key" : "\"{\"key1\":\"val1\",\"key2\":\"value2\"}\""
}
----------------------------------------------------------
@artur-kovalchuk
Copy link
Author

public static void main(String[] args) throws IOException {
    String jsonString = "{\"key1\":\"val1\",\"key2\":\"value2\"}";
    JSONObject jsonObject = new JSONObject(jsonString);

// JSONObject newJSON = jsonObject.getJSONObject("stat");
// System.out.println(newJSON.toString());
// jsonObject = new JSONObject(newJSON.toString());
// System.out.println(jsonObject.getString("rcv"));
// System.out.println(jsonObject.getJSONArray("argv"));

    ObjectMapper objectMapper = new ObjectMapper();
    Test test = objectMapper.readValue(jsonObject.toString(), Test.class);
    System.err.println(test);
}

@artur-kovalchuk
Copy link
Author

    <dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20160810</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.9.8</version>
    </dependency>

@artur-kovalchuk
Copy link
Author

write json in log

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment