How do I convert a Java Bean to a JsonObject?

wangzewei

I know that a JsonObject in the package org.json could create a JsonObject with a constructor whose argument is an object, but I could't find it in Eclipse.

As the image shows

After adding the org.json dependency, below is how it's showing up.

Madhu Bhat

The reason you are not able to see that constructor is since the JSONObject class that you are using is from the android-json package. You can see the same constructors in the Android JSONObject doc.

To get the JSONObject that you're expecting, you would need to add the dependency of org.json as below (either Maven or Gradle)

Maven dependency

<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20180813</version>
</dependency>

Gradle dependency

compile group: 'org.json', name: 'json', version: '20180813'

Then import this JSONObject class and you would see the constructor that you actually want.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I convert a LinkedTreeMap to gson JsonObject

How do I convert a JSONObject to a byte array and then convert this byte array to get back the original JSONObject?

How do I convert a string to a JSONObject in kotlin from openweather response?

How to convert JSONObject to Map in Java

How to convert a Java Object to a JSONObject?

How to convert jsonString to JSONObject in Java

How do I clone an org.json.JSONObject in Java?

How do I create a JSONObject?

How can I convert a JSONObject to a gson.JsonObject?

How can I convert a JsonObject to a JSON String?

How can I convert Date JsonObject to String?

How to convert a GoogleJsonResponseException into a JSONObject in android/java?

How would one convert a JSONObject into a DefaultMutableTreeNode in java?

how to convert stringified JSONObject[] to JSONObj[] in java

How do I convert json string into JsonObject and iterate on it? .NET Core 6.0 c#

How do I convert an Int to an Int[] in java?

How do I convert a number to a letter in Java?

How do I convert a TIF to PNG in Java?

How do I convert a String to an int in Java?

How do I convert an InputStream to a String in Java?

How do I convert a String to an InputStream in Java?

How do I convert number to a letter in Java?

How do I create a spring bean for a Java double primitive?

How do I use a @Transformer bean with Spring Integration Java DSL?

How do I parse a JSONObject into a list of integers?

How do I retrieve a JSONObject subfield?

In Gson, how do I add a JsonArray to a JsonObject?

How do I access a JSONObject subfield?

How do i resolve this JSONObject issue