1

I am trying to create json object for my data.

i found that, i can do that using two methods :-

put() and element()

please suggest me, which should be used.

my data is for example :-

key="id" value=32

Thanks in advance !!

Oleg
  • 220,925
  • 34
  • 403
  • 798
mayur
  • 47
  • 1
  • 3
  • 11
  • 2
    errrmmm ... your going to need to include more info than that ... what/where are the methods `put()` and `element()` - what language ? – Manse Feb 23 '12 at 15:02
  • Where are those methods coming from and what's wrong with `{id: 32}`? JSON is just a string, which is afterwards interpreted by JS engine to produce an object. Therefore, why would you need methods to construct a simple javascript array or object? – N.B. Feb 23 '12 at 15:03
  • hi, i am using spring framework to develop a web application. For that, i have to send the data from server to client and vice-versa. I am using "JSON" format for data transfer..... So, for this, i want to create a json object for data and that i will be sending to the client... To create these objects using data, on [JSON Doc](http://json-lib.sourceforge.net/apidocs/jdk15/index.html), they provided two methods, put() and element()... Now, i want to know, the difference between two methods. – mayur Feb 24 '12 at 06:24
  • You should include [java](http://stackoverflow.com/tags/java/info) and [json-lib](http://stackoverflow.com/tags/json-lib/info) tags to your question. The questions with [json](http://stackoverflow.com/tags/json/info) tag only should be used for the questions which are *independent* from any computer language and any library. I added the tags. – Oleg Mar 13 '12 at 09:41
  • I think it's highly likely this is his library, as I have had the same question: https://sourceforge.net/projects/json-lib – Reimius Jan 22 '13 at 20:56

1 Answers1

2

After inspecting the source code it seems that the differences betwee put and element are very minimal.

The main difference is that put appears to return the object that was previously at the key you entered's position in the properties map. So if you had a JSONObject structured like so:

{
    "steve": 4,
    "betty": 5
}

and executed a command like this:

Object frank = myJsonObject.put("steve", 10);

The value of frank would now be 4 and the json object would now look like this:

{
    "steve":10,
    "betty":5
}

If you had used .element("steve", 10); in the same situation, the object returned from the method is actually your JSONObject instead. The other difference between the two is that the first parameter to the put method is an Object and the first parameter to the element method is a String. The put method simply does a String.valueOf() on the first parameter sent into it and then calls the element method, so basically they both do the same thing, only put is more flexible and technically allows non-string keys that are then converted into strings before calling the element method.

In a nutshell, they have different parameters and return values, but the put method just calls the element method anyway, so there is not really a difference within the JSONObject, but possibly in your external code.

I'm guessing jQuery users would prefer to use element due to the similarities to that language in that the method returns the calling object.

Reimius
  • 5,694
  • 5
  • 24
  • 42
  • thank you for the info on this. i posted this last year, but i was not clear about the difference till now. – mayur Jan 31 '13 at 07:47
  • 1
    In case it's not obvious to anyone, since the `element` method returns a reference to the json object, it allows method chaining e.g. `myJsonObject.element("A","1").element("B","2").element("C","3")...` – RTF Dec 06 '14 at 15:03