JsonArray implemented by ArrayList under the hood. http://grepcode.com/file/repo1.maven.org/maven2/org.json/json/20080701/org/json/JSONArray.java

So its very similar variants for app performance. Use what you like =)

Answer from mlevytskiy on Stack Overflow
Discussions

android - Converting JSONarray to ArrayList - Stack Overflow
I am downloading a JSON string and converting it to JSONArray. Im putting it into a listview and need to be able to delete from that listview later, and since JSONArray has no .remove method (Thanks More on stackoverflow.com
🌐 stackoverflow.com
java - List vs JSONArray - Stack Overflow
List gives me the flexibility of looping through using for-each loop, while JSONArray will give JSONException when using getJSONObject method. More on stackoverflow.com
🌐 stackoverflow.com
February 8, 2018
java - JsonArray to Arraylist Conversion - Stack Overflow
You can use this block of code to convert JSONArray to the arraylist object. More on stackoverflow.com
🌐 stackoverflow.com
July 29, 2019
android - convert ArrayList to JSONArray - Stack Overflow
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 More on stackoverflow.com
🌐 stackoverflow.com
Top answer
1 of 2
3

JSONArray internally uses ArrayList. It's just wrapper over ArrayList. So I'd say there is no difference. JSONObject uses HashMap so again no real drawbacks.

2 of 2
0

In summary, JSON (which can be thought of a subset of JavaScript) is a lot leaner than XML. This has several positive side-effects

  • JSON is smaller than corresponding XML
  • JSON is faster, i.e. simpler syntax -> easier parsing (faster parsing)

JSON was that of JavaScript, I considered it to be a close relative. But JSON is something independent and JSON.org does a great job of describing JSON. It's also provides a compatibility library for JavaScript that adds support for JSON.parse and JSON.stringify when not supported by browsers.

While eval at the time (mid 2009) was used to evaluate JavaScript, it could also evaluate JSON, i.e. parse JSON, but it was considered unsafe, as it did allow arbitrary JavaScript to execute in its stead.

JSON just happens to be a very good fit for browsers and a natural way to evolve the platform due to its close relationship with JavaScript.

While XML might be considered to have better rigor due to the fact that you can type it, it is also those things that make it a lot slower (it is also a bit verbose in my opinion). But if this is something you really want, you should use it, XML is equally ubiquitous.

I will not go into a debate over dynamic or statically typed, but I will say this. It's really easy to add stuff on top of schema-free data and there are plenty of ways to do validation, regardless of schema or no schema.

Top answer
1 of 16
204
ArrayList<String> listdata = new ArrayList<String>();     
JSONArray jArray = (JSONArray)jsonObject; 
if (jArray != null) { 
   for (int i=0;i<jArray.length();i++){ 
    listdata.add(jArray.getString(i));
   } 
} 
2 of 16
85

I've done it using Gson (by Google).

Add the following line to your module's build.gradle:

dependencies {
  // ...
  // Note that `compile` will be deprecated. Use `implementation` instead.
  // See https://stackoverflow.com/a/44409111 for more info
  implementation 'com.google.code.gson:gson:2.8.2'
}

JSON string:

private String jsonString = "[\n" +
            "        {\n" +
            "                \"id\": \"c200\",\n" +
            "                \"name\": \"Ravi Tamada\",\n" +
            "                \"email\": \"[email protected]\",\n" +
            "                \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
            "                \"gender\" : \"male\",\n" +
            "                \"phone\": {\n" +
            "                    \"mobile\": \"+91 0000000000\",\n" +
            "                    \"home\": \"00 000000\",\n" +
            "                    \"office\": \"00 000000\"\n" +
            "                }\n" +
            "        },\n" +
            "        {\n" +
            "                \"id\": \"c201\",\n" +
            "                \"name\": \"Johnny Depp\",\n" +
            "                \"email\": \"[email protected]\",\n" +
            "                \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
            "                \"gender\" : \"male\",\n" +
            "                \"phone\": {\n" +
            "                    \"mobile\": \"+91 0000000000\",\n" +
            "                    \"home\": \"00 000000\",\n" +
            "                    \"office\": \"00 000000\"\n" +
            "                }\n" +
            "        },\n" +
            "        {\n" +
            "                \"id\": \"c202\",\n" +
            "                \"name\": \"Leonardo Dicaprio\",\n" +
            "                \"email\": \"[email protected]\",\n" +
            "                \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
            "                \"gender\" : \"male\",\n" +
            "                \"phone\": {\n" +
            "                    \"mobile\": \"+91 0000000000\",\n" +
            "                    \"home\": \"00 000000\",\n" +
            "                    \"office\": \"00 000000\"\n" +
            "                }\n" +
            "        },\n" +
            "        {\n" +
            "                \"id\": \"c203\",\n" +
            "                \"name\": \"John Wayne\",\n" +
            "                \"email\": \"[email protected]\",\n" +
            "                \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
            "                \"gender\" : \"male\",\n" +
            "                \"phone\": {\n" +
            "                    \"mobile\": \"+91 0000000000\",\n" +
            "                    \"home\": \"00 000000\",\n" +
            "                    \"office\": \"00 000000\"\n" +
            "                }\n" +
            "        },\n" +
            "        {\n" +
            "                \"id\": \"c204\",\n" +
            "                \"name\": \"Angelina Jolie\",\n" +
            "                \"email\": \"[email protected]\",\n" +
            "                \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
            "                \"gender\" : \"female\",\n" +
            "                \"phone\": {\n" +
            "                    \"mobile\": \"+91 0000000000\",\n" +
            "                    \"home\": \"00 000000\",\n" +
            "                    \"office\": \"00 000000\"\n" +
            "                }\n" +
            "        },\n" +
            "        {\n" +
            "                \"id\": \"c205\",\n" +
            "                \"name\": \"Dido\",\n" +
            "                \"email\": \"[email protected]\",\n" +
            "                \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
            "                \"gender\" : \"female\",\n" +
            "                \"phone\": {\n" +
            "                    \"mobile\": \"+91 0000000000\",\n" +
            "                    \"home\": \"00 000000\",\n" +
            "                    \"office\": \"00 000000\"\n" +
            "                }\n" +
            "        },\n" +
            "        {\n" +
            "                \"id\": \"c206\",\n" +
            "                \"name\": \"Adele\",\n" +
            "                \"email\": \"[email protected]\",\n" +
            "                \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
            "                \"gender\" : \"female\",\n" +
            "                \"phone\": {\n" +
            "                    \"mobile\": \"+91 0000000000\",\n" +
            "                    \"home\": \"00 000000\",\n" +
            "                    \"office\": \"00 000000\"\n" +
            "                }\n" +
            "        },\n" +
            "        {\n" +
            "                \"id\": \"c207\",\n" +
            "                \"name\": \"Hugh Jackman\",\n" +
            "                \"email\": \"[email protected]\",\n" +
            "                \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
            "                \"gender\" : \"male\",\n" +
            "                \"phone\": {\n" +
            "                    \"mobile\": \"+91 0000000000\",\n" +
            "                    \"home\": \"00 000000\",\n" +
            "                    \"office\": \"00 000000\"\n" +
            "                }\n" +
            "        },\n" +
            "        {\n" +
            "                \"id\": \"c208\",\n" +
            "                \"name\": \"Will Smith\",\n" +
            "                \"email\": \"[email protected]\",\n" +
            "                \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
            "                \"gender\" : \"male\",\n" +
            "                \"phone\": {\n" +
            "                    \"mobile\": \"+91 0000000000\",\n" +
            "                    \"home\": \"00 000000\",\n" +
            "                    \"office\": \"00 000000\"\n" +
            "                }\n" +
            "        },\n" +
            "        {\n" +
            "                \"id\": \"c209\",\n" +
            "                \"name\": \"Clint Eastwood\",\n" +
            "                \"email\": \"[email protected]\",\n" +
            "                \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
            "                \"gender\" : \"male\",\n" +
            "                \"phone\": {\n" +
            "                    \"mobile\": \"+91 0000000000\",\n" +
            "                    \"home\": \"00 000000\",\n" +
            "                    \"office\": \"00 000000\"\n" +
            "                }\n" +
            "        },\n" +
            "        {\n" +
            "                \"id\": \"c2010\",\n" +
            "                \"name\": \"Barack Obama\",\n" +
            "                \"email\": \"[email protected]\",\n" +
            "                \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
            "                \"gender\" : \"male\",\n" +
            "                \"phone\": {\n" +
            "                    \"mobile\": \"+91 0000000000\",\n" +
            "                    \"home\": \"00 000000\",\n" +
            "                    \"office\": \"00 000000\"\n" +
            "                }\n" +
            "        },\n" +
            "        {\n" +
            "                \"id\": \"c2011\",\n" +
            "                \"name\": \"Kate Winslet\",\n" +
            "                \"email\": \"[email protected]\",\n" +
            "                \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
            "                \"gender\" : \"female\",\n" +
            "                \"phone\": {\n" +
            "                    \"mobile\": \"+91 0000000000\",\n" +
            "                    \"home\": \"00 000000\",\n" +
            "                    \"office\": \"00 000000\"\n" +
            "                }\n" +
            "        },\n" +
            "        {\n" +
            "                \"id\": \"c2012\",\n" +
            "                \"name\": \"Eminem\",\n" +
            "                \"email\": \"[email protected]\",\n" +
            "                \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
            "                \"gender\" : \"male\",\n" +
            "                \"phone\": {\n" +
            "                    \"mobile\": \"+91 0000000000\",\n" +
            "                    \"home\": \"00 000000\",\n" +
            "                    \"office\": \"00 000000\"\n" +
            "                }\n" +
            "        }\n" +
            "    ]";

ContactModel.java:

public class ContactModel {
     public String id;
     public String name;
     public String email;
}

Code for converting a JSON string to ArrayList<Model>:

Note: You have to import java.lang.reflect.Type;:

// Top of file
import java.lang.reflect.Type;

// ...

private void parseJSON() {
    Gson gson = new Gson();
    Type type = new TypeToken<List<ContactModel>>(){}.getType();
    List<ContactModel> contactList = gson.fromJson(jsonString, type);
    for (ContactModel contact : contactList){
        Log.i("Contact Details", contact.id + "-" + contact.name + "-" + contact.email);
    }
}

Hope this will help you.

🌐
GitHub
gist.github.com › 21ec4a3a1fe9dc52e2cd195479428f98
JAVA array or ArrayList to JsonArray (Gson) · GitHub
JAVA array or ArrayList to JsonArray (Gson). GitHub Gist: instantly share code, notes, and snippets.
🌐
W3Schools
w3schools.com › js › js_json_arrays.asp
W3Schools.com
Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, Python, PHP, Bootstrap, Java, XML and more.
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › how-to-convert-java-array-or-arraylist-to-jsonarray-using-gson-in-java
How to convert Java array or ArrayList to JsonArray using Gson in Java?
November 4, 2023 - The Java Arrays are objects which store multiple variables of the same type, it holds primitive types and object references and an ArrayList can represent a resizable list of objects. We can add, remove, find, sort and replace elements using the list. A JsonArray can parse text from a string ...
🌐
BeginnersBook -
beginnersbook.com › home › java › convert json array to arraylist in java
Convert JSON Array to ArrayList in Java
June 13, 2024 - The object of JSONArray is created by passing jsonArrayString in the constructor of JSONArray class. The ArrayList that we have created is of JSONObject type, each element of this ArrayList represents the element of JSONArray.
🌐
How to do in Java
howtodoinjava.com › home › convert json array to list: gson, jackson and org.json
Convert JSON Array to List: Gson, Jackson and Org.json
August 10, 2014 - When working with JSON data, it’s often required to convert JSON arrays into Java lists/arraylists to work with the data more efficiently.
🌐
Google Groups
groups.google.com › g › vertx › c › 5fgz-e2rTbk
casting a JsonArray to a specific type
Since JsonArray returns List and not List<T> you only have two options: suppress the warnings or manually convert the list in a foreach loop.
🌐
Oracle
docs.oracle.com › javaee › 7 › api › javax › json › JsonArray.html
JsonArray (Java(TM) EE 7 Specification APIs)
JsonArray represents an immutable JSON array (an ordered sequence of zero or more values).
🌐
Reddit
reddit.com › r/learnprogramming › json array list help
r/learnprogramming on Reddit: JSON array list help
August 17, 2024 -

Hello,

How do I copy and paste a list of words into a JSON thing and have them come out as different items on this website? https://mytierlist.com/app/create . If you scroll down there's an option to edit as text

I have never coded in my life so I have no clue how to do this. And I don't know where to post this.

Im wanting to make a long list (over 1000+ words) and rank them on the website

And I’d like to just copy and paste all of the words at once and paste them in, and then have them become separate items in a second

I believe I use an array, and Ive found these websites that help convert lists to the format but I keep getting errors

e.g On this website https://jsonformatter.curiousconcept.com/# I paste the list of words in, but the quotation marks start at the beginning of the first word of the list and finish on the last word of the list, there are no quotation marks in-between or commas to separate the individual words

e.g on this website https://csvjson.com/csv2json It works very well when I paste the list in, when I select Hash and Parse JSON, but I don't want those brackets or colons, just quotation marks and commas to divide them. It would take too long to delete the brackets and colons

does anyone know any other websites that convert a list into a quotation mark and comma format?

Or another solution to this?

Top answer
1 of 1
1
You’d generally use a quick script to change format—e.g., if it’s just whtespace-separated plaintext, then Gawk (for gsub) would work nicely: #!/usr/bin/env gawk -f BEGIN {printf("["); pre=""} { gsub(/[\"\\]/, "\\\\&") gsub(/[\t\n\v\f\r]+/, " ") gsub(/[^[:print:]]/, "") for(i = 1; i <= NF; i+=length(pre=",")) printf("%s\"%s\"", pre, $i) } END {print "]"} Throw that in a file (e.g., txt2json.awk), chmod 0755 (assuming Unix or Cygwin; for plain Win …have fun lol) the file so you don’t have to name the interpreter every time you use it, and run it as unset LC_ALL export LC_CTYPE=C LC_COLLATE=C # ↑ This ensures that everything deals in bytes, rather than attempting to decode # characters, since I don’t know how your locale stuff is set up. ./txt2json.awk INPUT_FILE | tee OUTPUT_FILE.json or just use >OUTPUT_FILE instead of |tee… if you don’t want to watch the output tick by. If you’re starting from the clipboard, leave off INPUT_FILE and paste into the terminal directly. If you want to process words from a URL, you can potentially even curl https://foo.com/words.txt | ./txt2json.awk >OUTPUT.json or wget -O - https://foo.com/words.txt | ./txt2json etc… to elide any intermediate input. If you’re unfamiliar with Awk, it’s a tool that comes with every Unix/UNIX back to 19dickety2 (introduced, I assume, either in V7 or Research line, but maybe it’s BSD), which processes text files, preferring a line-by-line, then field-by-field approach. Gawk is the GNU version that supports gsub and gensub, and you probably have it linked to Awk if you have an Awk command at all. (gawk --version to check for Gawk.) De scriptibus: The shebang #! line tells the OS what to do with the file if it’s exec’d directly (which is what the chmod enables); if the file is txt2json, then it will run as /usr/bin/env gawk -f txt2json. env is used to search PATH for Gawk, rather than specifying absolutely, and gawk -f tells Gawk to read the script from a (this) file rather than the command line (e.g., gawk 'BEGIN {print "Hola, mundo"}'). Scripts comprise a list of match statements of the form [CONDITION]{ACTION}, along with other goodies like functions and includes; for each line of input, execution will start at the top and move downwards, running any ACTION whose CONDITION is nonzero, nonnil, or omitted. BEGIN matches once when Awk starts, before any files are processed, and the action here will print your opening [ and clear pre to nil, which is what we’ll print before the next word. There is no condition for the next action, so it runs once per line. Special variable $0 holds the entire line as a string, and $𝑖 for 𝑖≥1 gives you the 𝑖th field in the line, as divvied by whitespace by default. The gsub builtin replaces all matches of a regex in a string, and since no string is specified, it alters $0. JSON strings require \ and live quotes to be escaped, so ["\\] matches each " or \ in $0, and the replacement is the escaped form of \\&, denoting \\ followed by the matched character. (& is eqv to \0 in most other regex syntaxes, incl. gensub.) The next gsub normalizes whitespace—this also corrects issues with Win↔Unix line ending conversion. All runs of whitespace chars that aren’t exactly SP@U+0020 are replaced by a single space. ($0 will nominally re-split after this, so $𝑖s remain accurate when queried.) For the final gsub, all nonprinting characters ([^[:print]] ≡ “a character which is not in class print/is nonprinting—↔C/++ isprint) are deleted outright. This is something you probably want to tweak b/c any non-ASCII chars like ïèøæ’çčšß (as in e.g. naïve, façade, Weißmann, t’Hooft) will be deleted as a result. (I don’t know or care to know your encoding, and this is the safest default.) If you set LC_CTYPE to something UTF-8–supportive like en_US.UTF-8, you might fix it, but it’s better to escape any extended chars so your output is ASCII-clean. With an extra for loop, possibly a UTF-8 decode, and sprintf("\\u%04X\n", codepoint) you can do this reasonably easily, but …exercise for reader. The for loop should be syntactically semi-familiar, atl—once you get into it, Awk is not unJSlike because it’s also in the (over)extended C family. The loop sweeps i from index 1 (Awk is 1-based, unlike JS/C) to NF, a builtin which holds the number of fields. After each field, i will be incremented by 1, and pre will be set to , if it’s not already. (Awk has no comma operator—in C, the update would be i++, pre = ","—so I used a dummy length to incorporate the string update into the increment expression.) The loop body prints pre (initially "", then ","), followed by the double-quoted field text. END matches Awk exiting, and it prints the closing JSON ] followed by a newline.
🌐
Kodular
community.kodular.io › extensions › extension development
How to convert JSONArray to List? - Extension Development - Kodular Community
December 5, 2024 - I’m trying to create an extension based on an API, I’ve tried in several ways, but I’m not able to get the information that is inside each tag “name”. String genres = ""; JSONArray jsonArray = json.getJSONArray("genres"); genres = String.valueOf(jsonArray); Retorna : “genres”:[{“id”:878,“name”:“Ficção científica”},{“id”:28,“name”:“Ação”},{“id”:12,“name”:“Aventura”} I want it to return only the information that is inside the “name” Ficção científica Ação Aventura
🌐
Quora
quora.com › What-is-the-difference-between-json-array-and-json-object
What is the difference between json array and json object? - Quora
Answer (1 of 8): Arrays in JSON are used to organize a collection of related items (Which could be JSON objects). For example: [code ][{"name":"Name 1"},{"name": "Name 2} ][/code] On the other hand, you would use [code ]JSONObject[/code] when dealing with JSON that begins with curly braces. A J...
🌐
Ohandroid
ohandroid.com › jsonarray-vs-arraylist-x431.html
Использование JsonArray vs ArrayList в качестве набора данных для адаптера RecyclerView Oh! Android
JSONArray внутренне использует ArrayList . Это просто оболочка над ArrayList . Поэтому я бы сказал, что нет никакой разницы.