美文网首页KotlinjsonKotlin
Kotlin 实现 Json key首字母大写功能[番外]

Kotlin 实现 Json key首字母大写功能[番外]

作者: ys尘笑 | 来源:发表于2017-09-22 14:00 被阅读53次
    某人随手发的小题目
    class JsonTestUtil {
    
        fun upCaseKey(json: JSONObject): JSONObject {
            var key: String?
            val keys = ArrayList<String>()
            json.keys().forEach {
                keys.add(it as String)
            }
            keys.forEach {
                if (Character.isLowerCase(it[0])) {
                    key = it
                    val value = json.get(key)
                    json.remove(key)
                    key = upCaseKeyFirstChar(key)
                    json.put(key, value)
                }
            }
            json.keys().forEach {
                val value = json.get(it as String)
                when (value) {
                    is JSONArray -> {
                        if (value.length() > 0) {
                            for (i in 0..value.length() - 1) {
                                    upCaseKey(value[i] as JSONObject)
                            }
                        }
                    }
                    is JSONObject -> {
                        upCaseKey(value)
                    }
                }
            }
            return json
        }
    
        private fun upCaseKeyFirstChar(key: String?): String? {
            return if (Character.isUpperCase(key!![0])) {
                key
            } else {
                StringBuilder().append(Character.toUpperCase(key[0])).append(key.substring(1)).toString()
            }
        }
    
        fun testUpCaseKey(): Boolean {
            try {
                var json = JSONObject("{" +
                        "    \"test1\": \"1\"," +
                        "    \"testList\": [" +
                        "        {\n" +
                        "            \"test2\": 2," +
                        "            \"test3\": \"3\"" +
                        "        }\n" +
                        "    ],\n" +
                        "    \"testObj\": {" +
                        "        \"test4\": \"4\"," +
                        "        \"test5\": \"5\"," +
                        "        " +
                        "    }" +
                        "}")
                print("\ntestUpCaseKey input:" + json.toString())
                val time1 = System.currentTimeMillis()
    //            print("testUpCaseKey time1:" + time1)
                json = JsonTestUtil().upCaseKey(json)
                val time2 = System.currentTimeMillis()
    //            print("testUpCaseKey time2:" + time2)
                print("\ntestUpCaseKey result:" + json.toString())
                print("\ntestUpCaseKey time:" + (time2 - time1))
                return true
            } catch (e: JSONException) {
                e.printStackTrace()
                return false
            }
        }
    }
    
    Junit测试结果

    其实也是写在Kotlin练手项目里的,使用org的json只是因为这样比较规范?换成Fastjson也一样了。
    某人后来说他的想法是直接字符串处理,我想那样的话处理逻辑有点复杂了,这个逼就让他自己装吧。

    相关文章

      网友评论

        本文标题:Kotlin 实现 Json key首字母大写功能[番外]

        本文链接:https://www.haomeiwen.com/subject/xckfextx.html