Questions tagged [jsonobject]

A JSON Object is a textual representation of an object in JSON format as defined by RFC 7159 section 4; it is an unordered collection of name/value pairs (called "object members"), where names are JSON Strings and values can be any JSON value.

A JSON Object is a textual representation of an object in JSON format as defined by RFC 7159 section 4; it is an unordered collection of name/value pairs (called "object members"), where names are JSON Strings and values can be any JSON value.

Example (more below): {"answer":42}

Once the JSON text is parsed, the type of the resulting structure will vary widely depending on the environment being used. Some languages such as JavaScript, Python, and Ruby have builtin support for (nested) "dictionaries" and so the result will be a native structure. Other languages such as C, C++ and Java do not have such builtin types and represent the parsed result using dedicated classes.

Notes:

  • A member name can be an empty string; this is valid JSON: { "": null }.
  • Duplicate member names are allowed but the behaviour of a JSON parser in this case is undefined; for instance, in { "key": "v1", "key": "v2" }, there is no guarantee as to what the JSON parser will produce as a value for "key".
  • JSON is a textual notation for data exchange. If you're writing source code and not dealing with a string, you're not dealing with JSON anymore, you're dealing with the result of parsing JSON. This can be confusing in languages that can't map a JSON Object to a built-in structure, like Java, as the names of the classes involved can be confusing (JSONObject in the case of many JSON libraries for Java). Despite the name, it's no longer JSON; it's just the result of parsing JSON.
  • JavaScript object initializers are not the same as JSON Objects, e.g., this is not JSON: var obj = {"foo":"bar"}; JavaScript object initializers have slightly different semantics than JSON (for instance, member names need not be in quotes, values that do not exist in JSON such as undefined can be included, duplicate member names have a defined result both in loose and strict mode, member names can be the result of a calculation [in ES2015 and later], etc.).

Examples:

An object with one member, called answer, whose value is 42:

{"answer":42}

An identical object with whitespace that's ignored in JSON:

{
    "answer" : 42
}

An object with two members:

{"answer":42,"question":"Life, the Universe, and Everything"}

An object with another object as the value of one of its members:

{
    "title": "The Hitchhiker's Guide to the Galaxy",
    "author": {
        "name": "Douglas Adams",
        "website": "http://douglasadams.com/",
        "wikipedia": "https://en.wikipedia.org/wiki/Douglas_Adams"
    }
}
735 questions
123
votes
7 answers

convert ArrayList to JSONArray

I have an ArrayList that I use within an ArrayAdapter for a ListView. I need to take the items in the list and convert them to a JSONArray to send to an API. I've searched around, but haven't found anything that explains how this might work, any…
pmko
  • 4,901
  • 3
  • 21
  • 24
61
votes
6 answers

Convert JSONObject to Map

I have a JSONObject with some attributes that I want to convert into a Map Is there something that I can use from the json.org or ObjectMapper?
Marco C
  • 2,631
  • 3
  • 26
  • 45
47
votes
4 answers

Serialize multiple forms together?

Can you serialize multiple forms into one so that only one post or ajax request is made? I have searched around and it is all for submiting each form separently via post/ajax.
arbme
  • 4,443
  • 11
  • 39
  • 53
35
votes
2 answers

Delete an element in a JSON object

I am trying to loop through a list of objects deleting an element from each object. Each object is a new line. I am trying to then save the new file as is without the element contained within the objects. I know this is probably a simple task but I…
Bradley
  • 754
  • 1
  • 6
  • 19
35
votes
12 answers

Volley JsonObjectRequest Post parameters no longer work

I am trying to send POST parameters in a Volley JsonObjectRequest. Initially, it was working for me by following what the official code says to do of passing a JSONObject containing the parameters in the constructor of the JsonObjectRequest. Then…
Joshua C.
  • 801
  • 1
  • 9
  • 12
31
votes
2 answers

Creating nested JSON object for the following structure in Java using JSONObject?

I want to build a JSON Object similar to following the structure in java using JSONObject and JSONArray. I have gone through various posts in stack overflow, which suggests using methods like push, put etc which I am unable to identify for…
user3131769
  • 335
  • 1
  • 3
  • 9
27
votes
1 answer

How do I convert a LinkedTreeMap to gson JsonObject

For a java data handler, I send properly formatted JSON, but a combination of Spring, Java deciding how to cast what it sees, and frameworks I really shouldn't go changing mangle that JSON so that once I can see it, it's turned into a LinkedTreeMap,…
Bronanaza
  • 683
  • 2
  • 6
  • 16
21
votes
4 answers

Java GSON: Getting the list of all keys under a JSONObject

I have got GSON as a JSON parser in Java, but the keys aren't always the same. For example. I have the following JSON: { "The Object I already know": { "key1":"value1", "key2":"value2", "AnotherObject": {…
imthe666st
  • 323
  • 1
  • 2
  • 6
18
votes
3 answers

How to convert a Java Object to a JSONObject?

i need to convert a POJO to a JSONObject (org.json.JSONObject) I know how to convert it to a file: ObjectMapper mapper = new ObjectMapper(); try { mapper.writeValue(new File(file.toString()), registrationData); } catch…
Kaloyan Roussev
  • 13,557
  • 18
  • 83
  • 165
15
votes
4 answers

How to compare two JSON strings when the order of entries keep changing

I have a string like - {"state":1,"cmd":1} , I need to compare this with generated output but in the generated output the order keeps changing i.e. sometimes its {"state":1,"cmd":1} other times its {"cmd":1,"state":1}. Currently I was using…
Green
  • 523
  • 3
  • 5
  • 13
13
votes
2 answers

React.js creating a table with a dynamic amount of rows with an editable column

I am trying to create a react.js table that syncs with a leaflet map. I have this data and I am able to get the data properly, but I cannot create a table correctly. I am able to see the headers because they are hardcoded, but I am not able to see…
napkinsterror
  • 1,685
  • 4
  • 16
  • 26
12
votes
4 answers

Converting JSONObject to Java Object

I made a rest call to a service and stored the response in a JSONObject. However, I am trying to convert it to a class object and getting errors. Here's my code: RestOperations operations = /*initalize*/; String body = /*build request body*/; String…
Richard
  • 5,010
  • 24
  • 106
  • 182
12
votes
3 answers

Post Json object data to get Json array response using volley in android

I need to post JSONObject (using Volley) to a web-service which is returning the response in JSONArray format. Here is what I have tried so far. final JSONObject requestJsonObject = new JSONObject(); requestJsonObject.put("username",…
Santhosh
  • 4,696
  • 11
  • 53
  • 86
12
votes
2 answers

Fragment crashes with Parcel: unable to marshal value error when onPause method is called

I have been developing android apps for a while now but only recently gotten into Fragments and hence I run into a lot of problems learning how to work with them. One of the activities in my applications has four different fragments, each of which…
11
votes
3 answers

JSONObject get value of first node regardless of name

I am wondering if there is a way to get the value of the first child of a JSONObject without knowing its name: I have some JSON coming in with a node called, this_guy {"this_guy": {"some_name_i_wont_know":"the value i care about"}} Using…
erik
  • 4,698
  • 11
  • 64
  • 111
1
2 3
48 49