Java – inserts an object into a specific location in jsonarray

I want to add objects to a specific location in jsonarray. My current jsonarray looks like this

{
    "imgs": [
        "String1",
        "String2",
        "String3",
        "String4"
    ]
}

I need to insert a project in jsonarray in the first location, as shown below –

jsonArray.put(1,"String5")

This is the replacement item in the first place, but I need the following results

{
    "imgs": [
        "String1",
        "String5",
        "String2",
        "String3",
        "String4"
    ]
}

Please suggest

resolvent:

Android jsonarray is not a universal data structure, but (DE) serializes data to and from JSON. Therefore, you should perform all add / remove / mutate operations on java.util.list or other content, and only when you want to send it over the line and convert it to jsonarray

Java EE 7 also has a jsonarray class. In their API description, they clearly indicate that they want to prevent people from using it as a substitute for java.util.list: jsonarray instances are list objects that provide read-only access to java.util.list. Values in JSON arrays. Whether directly modifying the list or using its collection view, Any attempt to modify the list will result in unsupported operationexception

See also this answer

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>