How to define only JSONObject with its keys

I want to create a JSON object and define its keys. I will initialize the values ​​later. Is there any way to do this? I want something like this when I print a JSONObject before initializing it and defining it:

education: {"name": "," year ":," qualification ":}

ie only keys exist, no values ​​exist for them. how to define this object of "education". I know that his defense may be:

JSONObject education = new JSONObject ();

but where do the keys fit in here?

+5
source share
2 answers

You simply cannot have specific keys defined. What you are essentially trying to say is that I want these fields, but not defining the type of these fields. Therefore, even assuming that it was hypothetically allowed, how would you know what to expect as a value for any of the fields.

education:{ "name":"", "year":, "qualification": } 

From all this declaration we cannot determine the name again the JSON object? or string field? the same thing happens with other fields.

Thus, the solution to your request will have default values ​​for these fields. Ex. if the string has a "" (empty string) , if the object is null .

Then you can change them as and when you get the actual values.

+1
source

You can simply create a JSON object and then put each attribute in an empty string.

here is the code:

 JSONObject education = new JSONObject(); education.put("name",new String()); education.put("year",new String()); education.put("qualification",new String()); 
+2
source

Source: https://habr.com/ru/post/1273648/


All Articles