Java - Convert HTML Parameters to JSON












0















I do work for a project currently, where the data is send to the server as application/x-www-form-urlencoded (which is bad, and it should be JSON, but unfortunately I am not able to change this one).



My question is, how can I transfer the given structure to JSON, or, even better, deserialize it directly to an object?



_id=[5bfad95450642c333010daca], 
_rev=[1-9ce33949c3acd85cea6c58467e6a8144],
type=[Group],
user=[aUSer],
default=[aDetail],
store[aDetail][prop]=[5],
store[aDetail][lprop1][0][time]=[00:00],
store[aDetail][lprop1][0][value]=[14],
store[aDetail][lprop1][0][timeAsSeconds]=[0],
store[aDetail][lprop1][1][time]=[07:00],
store[aDetail][lprop1][1][value]=[8],
store[aDetail][lprop1][1][timeAsSeconds]=[25200],
store[aDetail][anprop]=[25],
store[aDetail][lprop2][0][time]=[00:00],
store[aDetail][lprop2][0][value]=[61],
store[aDetail][lprop2][0][timeAsSeconds]=[0],
store[bDetail][prop]=[6],
store[bDetail][lprop1][0][time]=[00:10],
store[bDetail][lprop1][0][value]=[12],
store[bDetail][lprop1][0][timeAsSeconds]=[0],
store[bDetail][lprop1][1][time]=[07:10],
store[bDetail][lprop1][1][value]=[9],
store[bDetail][lprop1][1][timeAsSeconds]=[25200],
store[bDetail][anprop]=[25],
store[bDetail][lprop2][0][time]=[00:00],
store[bDetail][lprop2][0][value]=[61],
store[bDetail][lprop2][0][timeAsSeconds]=[0],
created_at=[2018-01-11T20:48:22.574+0100]


In json, this would look something like this (skippen most of the already given values):



{
_id: 5bfad95450642c333010daca,
_rev: 1-9ce33949c3acd85cea6c58467e6a8144,
type: Group,
user: aUSer,
default: aDetail,
store: [
aDetail: {
prop: 0,
lprop1: [
{
time: 00:00,
value: 14,
timeAsSeconds: 0
}
]
}
]
}


The conversion from this form format to json is rather annoying. Any help is appreciated. BTW, me is using Jackson and Java and/or Kotlin, if this is of any help.










share|improve this question

























  • stackoverflow.com/questions/24551915/…

    – 宏杰李
    Dec 29 '18 at 0:44











  • You should give the input xml too.

    – NiksVij
    Dec 29 '18 at 2:18











  • NiksVij There is no Input XML, the question is just about the HTML Parameters, those are transferred over the wire like described above.

    – triplem
    Dec 29 '18 at 8:15
















0















I do work for a project currently, where the data is send to the server as application/x-www-form-urlencoded (which is bad, and it should be JSON, but unfortunately I am not able to change this one).



My question is, how can I transfer the given structure to JSON, or, even better, deserialize it directly to an object?



_id=[5bfad95450642c333010daca], 
_rev=[1-9ce33949c3acd85cea6c58467e6a8144],
type=[Group],
user=[aUSer],
default=[aDetail],
store[aDetail][prop]=[5],
store[aDetail][lprop1][0][time]=[00:00],
store[aDetail][lprop1][0][value]=[14],
store[aDetail][lprop1][0][timeAsSeconds]=[0],
store[aDetail][lprop1][1][time]=[07:00],
store[aDetail][lprop1][1][value]=[8],
store[aDetail][lprop1][1][timeAsSeconds]=[25200],
store[aDetail][anprop]=[25],
store[aDetail][lprop2][0][time]=[00:00],
store[aDetail][lprop2][0][value]=[61],
store[aDetail][lprop2][0][timeAsSeconds]=[0],
store[bDetail][prop]=[6],
store[bDetail][lprop1][0][time]=[00:10],
store[bDetail][lprop1][0][value]=[12],
store[bDetail][lprop1][0][timeAsSeconds]=[0],
store[bDetail][lprop1][1][time]=[07:10],
store[bDetail][lprop1][1][value]=[9],
store[bDetail][lprop1][1][timeAsSeconds]=[25200],
store[bDetail][anprop]=[25],
store[bDetail][lprop2][0][time]=[00:00],
store[bDetail][lprop2][0][value]=[61],
store[bDetail][lprop2][0][timeAsSeconds]=[0],
created_at=[2018-01-11T20:48:22.574+0100]


In json, this would look something like this (skippen most of the already given values):



{
_id: 5bfad95450642c333010daca,
_rev: 1-9ce33949c3acd85cea6c58467e6a8144,
type: Group,
user: aUSer,
default: aDetail,
store: [
aDetail: {
prop: 0,
lprop1: [
{
time: 00:00,
value: 14,
timeAsSeconds: 0
}
]
}
]
}


The conversion from this form format to json is rather annoying. Any help is appreciated. BTW, me is using Jackson and Java and/or Kotlin, if this is of any help.










share|improve this question

























  • stackoverflow.com/questions/24551915/…

    – 宏杰李
    Dec 29 '18 at 0:44











  • You should give the input xml too.

    – NiksVij
    Dec 29 '18 at 2:18











  • NiksVij There is no Input XML, the question is just about the HTML Parameters, those are transferred over the wire like described above.

    – triplem
    Dec 29 '18 at 8:15














0












0








0


1






I do work for a project currently, where the data is send to the server as application/x-www-form-urlencoded (which is bad, and it should be JSON, but unfortunately I am not able to change this one).



My question is, how can I transfer the given structure to JSON, or, even better, deserialize it directly to an object?



_id=[5bfad95450642c333010daca], 
_rev=[1-9ce33949c3acd85cea6c58467e6a8144],
type=[Group],
user=[aUSer],
default=[aDetail],
store[aDetail][prop]=[5],
store[aDetail][lprop1][0][time]=[00:00],
store[aDetail][lprop1][0][value]=[14],
store[aDetail][lprop1][0][timeAsSeconds]=[0],
store[aDetail][lprop1][1][time]=[07:00],
store[aDetail][lprop1][1][value]=[8],
store[aDetail][lprop1][1][timeAsSeconds]=[25200],
store[aDetail][anprop]=[25],
store[aDetail][lprop2][0][time]=[00:00],
store[aDetail][lprop2][0][value]=[61],
store[aDetail][lprop2][0][timeAsSeconds]=[0],
store[bDetail][prop]=[6],
store[bDetail][lprop1][0][time]=[00:10],
store[bDetail][lprop1][0][value]=[12],
store[bDetail][lprop1][0][timeAsSeconds]=[0],
store[bDetail][lprop1][1][time]=[07:10],
store[bDetail][lprop1][1][value]=[9],
store[bDetail][lprop1][1][timeAsSeconds]=[25200],
store[bDetail][anprop]=[25],
store[bDetail][lprop2][0][time]=[00:00],
store[bDetail][lprop2][0][value]=[61],
store[bDetail][lprop2][0][timeAsSeconds]=[0],
created_at=[2018-01-11T20:48:22.574+0100]


In json, this would look something like this (skippen most of the already given values):



{
_id: 5bfad95450642c333010daca,
_rev: 1-9ce33949c3acd85cea6c58467e6a8144,
type: Group,
user: aUSer,
default: aDetail,
store: [
aDetail: {
prop: 0,
lprop1: [
{
time: 00:00,
value: 14,
timeAsSeconds: 0
}
]
}
]
}


The conversion from this form format to json is rather annoying. Any help is appreciated. BTW, me is using Jackson and Java and/or Kotlin, if this is of any help.










share|improve this question
















I do work for a project currently, where the data is send to the server as application/x-www-form-urlencoded (which is bad, and it should be JSON, but unfortunately I am not able to change this one).



My question is, how can I transfer the given structure to JSON, or, even better, deserialize it directly to an object?



_id=[5bfad95450642c333010daca], 
_rev=[1-9ce33949c3acd85cea6c58467e6a8144],
type=[Group],
user=[aUSer],
default=[aDetail],
store[aDetail][prop]=[5],
store[aDetail][lprop1][0][time]=[00:00],
store[aDetail][lprop1][0][value]=[14],
store[aDetail][lprop1][0][timeAsSeconds]=[0],
store[aDetail][lprop1][1][time]=[07:00],
store[aDetail][lprop1][1][value]=[8],
store[aDetail][lprop1][1][timeAsSeconds]=[25200],
store[aDetail][anprop]=[25],
store[aDetail][lprop2][0][time]=[00:00],
store[aDetail][lprop2][0][value]=[61],
store[aDetail][lprop2][0][timeAsSeconds]=[0],
store[bDetail][prop]=[6],
store[bDetail][lprop1][0][time]=[00:10],
store[bDetail][lprop1][0][value]=[12],
store[bDetail][lprop1][0][timeAsSeconds]=[0],
store[bDetail][lprop1][1][time]=[07:10],
store[bDetail][lprop1][1][value]=[9],
store[bDetail][lprop1][1][timeAsSeconds]=[25200],
store[bDetail][anprop]=[25],
store[bDetail][lprop2][0][time]=[00:00],
store[bDetail][lprop2][0][value]=[61],
store[bDetail][lprop2][0][timeAsSeconds]=[0],
created_at=[2018-01-11T20:48:22.574+0100]


In json, this would look something like this (skippen most of the already given values):



{
_id: 5bfad95450642c333010daca,
_rev: 1-9ce33949c3acd85cea6c58467e6a8144,
type: Group,
user: aUSer,
default: aDetail,
store: [
aDetail: {
prop: 0,
lprop1: [
{
time: 00:00,
value: 14,
timeAsSeconds: 0
}
]
}
]
}


The conversion from this form format to json is rather annoying. Any help is appreciated. BTW, me is using Jackson and Java and/or Kotlin, if this is of any help.







java html json forms post






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 29 '18 at 8:57







triplem

















asked Dec 28 '18 at 23:24









triplemtriplem

406614




406614













  • stackoverflow.com/questions/24551915/…

    – 宏杰李
    Dec 29 '18 at 0:44











  • You should give the input xml too.

    – NiksVij
    Dec 29 '18 at 2:18











  • NiksVij There is no Input XML, the question is just about the HTML Parameters, those are transferred over the wire like described above.

    – triplem
    Dec 29 '18 at 8:15



















  • stackoverflow.com/questions/24551915/…

    – 宏杰李
    Dec 29 '18 at 0:44











  • You should give the input xml too.

    – NiksVij
    Dec 29 '18 at 2:18











  • NiksVij There is no Input XML, the question is just about the HTML Parameters, those are transferred over the wire like described above.

    – triplem
    Dec 29 '18 at 8:15

















stackoverflow.com/questions/24551915/…

– 宏杰李
Dec 29 '18 at 0:44





stackoverflow.com/questions/24551915/…

– 宏杰李
Dec 29 '18 at 0:44













You should give the input xml too.

– NiksVij
Dec 29 '18 at 2:18





You should give the input xml too.

– NiksVij
Dec 29 '18 at 2:18













NiksVij There is no Input XML, the question is just about the HTML Parameters, those are transferred over the wire like described above.

– triplem
Dec 29 '18 at 8:15





NiksVij There is no Input XML, the question is just about the HTML Parameters, those are transferred over the wire like described above.

– triplem
Dec 29 '18 at 8:15












1 Answer
1






active

oldest

votes


















0














I have a rather naive solution, which takes nested objects and arrays into account. This works for me, and there can/will be some major drawbacks, if used in other environments.



The solution is written in kotlin but most probably could be written in a not as complex way.



fun parseToMap(map: MutableMap<String, Any>, key: String, value: Any): MutableMap<String, Any> {
val cleanedV = if (value is String) URLDecoder.decode(value, "UTF-8") else value

if (!key.contains("[")) {
map.putIfAbsent(key, cleanedV)
} else {
// mapKey is the key going to get stored in the map
val mapKey = key.substring(0, key.indexOf("["))

// nextKey is the next key pushed to the next call of parseToMap
var nextKey = key.removePrefix(mapKey)
nextKey = nextKey.replaceFirst("[", "").replaceFirst("]", "")

var isArray = false
var index = -1

if (nextKey.contains("[") &&
nextKey.substring(0, nextKey.indexOf("[")).matches(Regex("[0-9]+"))) {
index = nextKey.substring(0, nextKey.indexOf("[")).toInt()
isArray = true
}

// mapkey used for object in list
val newMapKey = if (isArray) nextKey.substring(nextKey.indexOf("[") + 1, nextKey.indexOf("]")) else ""

val child: Any?
var childMap: MutableMap<String, Any> = mutableMapOf()

if (map.containsKey(mapKey)) {
println("key $mapKey exists already")
child = map[mapKey]

when (child) {
is MutableList<*> -> {
if (child == null || child.isEmpty()) {
childMap = mutableMapOf()
val tmpList = child as MutableList<Any>
tmpList.add(childMap)
map.put(newMapKey, tmpList)
} else {
if (child.size > index) {
childMap = child.get(index) as MutableMap<String, Any>
childMap = parseToMap(childMap, newMapKey, value)
} else {
childMap = parseToMap(childMap, newMapKey, value)
val tmpList = child as MutableList<Any>
tmpList.add(childMap)
}
}
}
is MutableMap<*, *> -> childMap = map.get(mapKey) as MutableMap<String, Any>
}
} else {
if (isArray) {
child = mutableListOf<Any>()
childMap = parseToMap(childMap, newMapKey, value)
child.add(childMap)
map.put(mapKey, child)
} else {
childMap = mutableMapOf<String, Any>()
}
}

if (!isArray) parseToMap(childMap, nextKey, value)

map.putIfAbsent(mapKey, childMap)
}

return map
}


This method should be called with a Map and each parameter key (like bespoken in my question already) as well as the value of each parameter.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53965357%2fjava-convert-html-parameters-to-json%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I have a rather naive solution, which takes nested objects and arrays into account. This works for me, and there can/will be some major drawbacks, if used in other environments.



    The solution is written in kotlin but most probably could be written in a not as complex way.



    fun parseToMap(map: MutableMap<String, Any>, key: String, value: Any): MutableMap<String, Any> {
    val cleanedV = if (value is String) URLDecoder.decode(value, "UTF-8") else value

    if (!key.contains("[")) {
    map.putIfAbsent(key, cleanedV)
    } else {
    // mapKey is the key going to get stored in the map
    val mapKey = key.substring(0, key.indexOf("["))

    // nextKey is the next key pushed to the next call of parseToMap
    var nextKey = key.removePrefix(mapKey)
    nextKey = nextKey.replaceFirst("[", "").replaceFirst("]", "")

    var isArray = false
    var index = -1

    if (nextKey.contains("[") &&
    nextKey.substring(0, nextKey.indexOf("[")).matches(Regex("[0-9]+"))) {
    index = nextKey.substring(0, nextKey.indexOf("[")).toInt()
    isArray = true
    }

    // mapkey used for object in list
    val newMapKey = if (isArray) nextKey.substring(nextKey.indexOf("[") + 1, nextKey.indexOf("]")) else ""

    val child: Any?
    var childMap: MutableMap<String, Any> = mutableMapOf()

    if (map.containsKey(mapKey)) {
    println("key $mapKey exists already")
    child = map[mapKey]

    when (child) {
    is MutableList<*> -> {
    if (child == null || child.isEmpty()) {
    childMap = mutableMapOf()
    val tmpList = child as MutableList<Any>
    tmpList.add(childMap)
    map.put(newMapKey, tmpList)
    } else {
    if (child.size > index) {
    childMap = child.get(index) as MutableMap<String, Any>
    childMap = parseToMap(childMap, newMapKey, value)
    } else {
    childMap = parseToMap(childMap, newMapKey, value)
    val tmpList = child as MutableList<Any>
    tmpList.add(childMap)
    }
    }
    }
    is MutableMap<*, *> -> childMap = map.get(mapKey) as MutableMap<String, Any>
    }
    } else {
    if (isArray) {
    child = mutableListOf<Any>()
    childMap = parseToMap(childMap, newMapKey, value)
    child.add(childMap)
    map.put(mapKey, child)
    } else {
    childMap = mutableMapOf<String, Any>()
    }
    }

    if (!isArray) parseToMap(childMap, nextKey, value)

    map.putIfAbsent(mapKey, childMap)
    }

    return map
    }


    This method should be called with a Map and each parameter key (like bespoken in my question already) as well as the value of each parameter.






    share|improve this answer




























      0














      I have a rather naive solution, which takes nested objects and arrays into account. This works for me, and there can/will be some major drawbacks, if used in other environments.



      The solution is written in kotlin but most probably could be written in a not as complex way.



      fun parseToMap(map: MutableMap<String, Any>, key: String, value: Any): MutableMap<String, Any> {
      val cleanedV = if (value is String) URLDecoder.decode(value, "UTF-8") else value

      if (!key.contains("[")) {
      map.putIfAbsent(key, cleanedV)
      } else {
      // mapKey is the key going to get stored in the map
      val mapKey = key.substring(0, key.indexOf("["))

      // nextKey is the next key pushed to the next call of parseToMap
      var nextKey = key.removePrefix(mapKey)
      nextKey = nextKey.replaceFirst("[", "").replaceFirst("]", "")

      var isArray = false
      var index = -1

      if (nextKey.contains("[") &&
      nextKey.substring(0, nextKey.indexOf("[")).matches(Regex("[0-9]+"))) {
      index = nextKey.substring(0, nextKey.indexOf("[")).toInt()
      isArray = true
      }

      // mapkey used for object in list
      val newMapKey = if (isArray) nextKey.substring(nextKey.indexOf("[") + 1, nextKey.indexOf("]")) else ""

      val child: Any?
      var childMap: MutableMap<String, Any> = mutableMapOf()

      if (map.containsKey(mapKey)) {
      println("key $mapKey exists already")
      child = map[mapKey]

      when (child) {
      is MutableList<*> -> {
      if (child == null || child.isEmpty()) {
      childMap = mutableMapOf()
      val tmpList = child as MutableList<Any>
      tmpList.add(childMap)
      map.put(newMapKey, tmpList)
      } else {
      if (child.size > index) {
      childMap = child.get(index) as MutableMap<String, Any>
      childMap = parseToMap(childMap, newMapKey, value)
      } else {
      childMap = parseToMap(childMap, newMapKey, value)
      val tmpList = child as MutableList<Any>
      tmpList.add(childMap)
      }
      }
      }
      is MutableMap<*, *> -> childMap = map.get(mapKey) as MutableMap<String, Any>
      }
      } else {
      if (isArray) {
      child = mutableListOf<Any>()
      childMap = parseToMap(childMap, newMapKey, value)
      child.add(childMap)
      map.put(mapKey, child)
      } else {
      childMap = mutableMapOf<String, Any>()
      }
      }

      if (!isArray) parseToMap(childMap, nextKey, value)

      map.putIfAbsent(mapKey, childMap)
      }

      return map
      }


      This method should be called with a Map and each parameter key (like bespoken in my question already) as well as the value of each parameter.






      share|improve this answer


























        0












        0








        0







        I have a rather naive solution, which takes nested objects and arrays into account. This works for me, and there can/will be some major drawbacks, if used in other environments.



        The solution is written in kotlin but most probably could be written in a not as complex way.



        fun parseToMap(map: MutableMap<String, Any>, key: String, value: Any): MutableMap<String, Any> {
        val cleanedV = if (value is String) URLDecoder.decode(value, "UTF-8") else value

        if (!key.contains("[")) {
        map.putIfAbsent(key, cleanedV)
        } else {
        // mapKey is the key going to get stored in the map
        val mapKey = key.substring(0, key.indexOf("["))

        // nextKey is the next key pushed to the next call of parseToMap
        var nextKey = key.removePrefix(mapKey)
        nextKey = nextKey.replaceFirst("[", "").replaceFirst("]", "")

        var isArray = false
        var index = -1

        if (nextKey.contains("[") &&
        nextKey.substring(0, nextKey.indexOf("[")).matches(Regex("[0-9]+"))) {
        index = nextKey.substring(0, nextKey.indexOf("[")).toInt()
        isArray = true
        }

        // mapkey used for object in list
        val newMapKey = if (isArray) nextKey.substring(nextKey.indexOf("[") + 1, nextKey.indexOf("]")) else ""

        val child: Any?
        var childMap: MutableMap<String, Any> = mutableMapOf()

        if (map.containsKey(mapKey)) {
        println("key $mapKey exists already")
        child = map[mapKey]

        when (child) {
        is MutableList<*> -> {
        if (child == null || child.isEmpty()) {
        childMap = mutableMapOf()
        val tmpList = child as MutableList<Any>
        tmpList.add(childMap)
        map.put(newMapKey, tmpList)
        } else {
        if (child.size > index) {
        childMap = child.get(index) as MutableMap<String, Any>
        childMap = parseToMap(childMap, newMapKey, value)
        } else {
        childMap = parseToMap(childMap, newMapKey, value)
        val tmpList = child as MutableList<Any>
        tmpList.add(childMap)
        }
        }
        }
        is MutableMap<*, *> -> childMap = map.get(mapKey) as MutableMap<String, Any>
        }
        } else {
        if (isArray) {
        child = mutableListOf<Any>()
        childMap = parseToMap(childMap, newMapKey, value)
        child.add(childMap)
        map.put(mapKey, child)
        } else {
        childMap = mutableMapOf<String, Any>()
        }
        }

        if (!isArray) parseToMap(childMap, nextKey, value)

        map.putIfAbsent(mapKey, childMap)
        }

        return map
        }


        This method should be called with a Map and each parameter key (like bespoken in my question already) as well as the value of each parameter.






        share|improve this answer













        I have a rather naive solution, which takes nested objects and arrays into account. This works for me, and there can/will be some major drawbacks, if used in other environments.



        The solution is written in kotlin but most probably could be written in a not as complex way.



        fun parseToMap(map: MutableMap<String, Any>, key: String, value: Any): MutableMap<String, Any> {
        val cleanedV = if (value is String) URLDecoder.decode(value, "UTF-8") else value

        if (!key.contains("[")) {
        map.putIfAbsent(key, cleanedV)
        } else {
        // mapKey is the key going to get stored in the map
        val mapKey = key.substring(0, key.indexOf("["))

        // nextKey is the next key pushed to the next call of parseToMap
        var nextKey = key.removePrefix(mapKey)
        nextKey = nextKey.replaceFirst("[", "").replaceFirst("]", "")

        var isArray = false
        var index = -1

        if (nextKey.contains("[") &&
        nextKey.substring(0, nextKey.indexOf("[")).matches(Regex("[0-9]+"))) {
        index = nextKey.substring(0, nextKey.indexOf("[")).toInt()
        isArray = true
        }

        // mapkey used for object in list
        val newMapKey = if (isArray) nextKey.substring(nextKey.indexOf("[") + 1, nextKey.indexOf("]")) else ""

        val child: Any?
        var childMap: MutableMap<String, Any> = mutableMapOf()

        if (map.containsKey(mapKey)) {
        println("key $mapKey exists already")
        child = map[mapKey]

        when (child) {
        is MutableList<*> -> {
        if (child == null || child.isEmpty()) {
        childMap = mutableMapOf()
        val tmpList = child as MutableList<Any>
        tmpList.add(childMap)
        map.put(newMapKey, tmpList)
        } else {
        if (child.size > index) {
        childMap = child.get(index) as MutableMap<String, Any>
        childMap = parseToMap(childMap, newMapKey, value)
        } else {
        childMap = parseToMap(childMap, newMapKey, value)
        val tmpList = child as MutableList<Any>
        tmpList.add(childMap)
        }
        }
        }
        is MutableMap<*, *> -> childMap = map.get(mapKey) as MutableMap<String, Any>
        }
        } else {
        if (isArray) {
        child = mutableListOf<Any>()
        childMap = parseToMap(childMap, newMapKey, value)
        child.add(childMap)
        map.put(mapKey, child)
        } else {
        childMap = mutableMapOf<String, Any>()
        }
        }

        if (!isArray) parseToMap(childMap, nextKey, value)

        map.putIfAbsent(mapKey, childMap)
        }

        return map
        }


        This method should be called with a Map and each parameter key (like bespoken in my question already) as well as the value of each parameter.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Dec 30 '18 at 14:00









        triplemtriplem

        406614




        406614






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53965357%2fjava-convert-html-parameters-to-json%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Monofisismo

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas