简体   繁体   English

在 kotlin 中将 String 转换为 JsonObject 返回 null

[英]Convert String to JsonObject in kotlin returns null

Edit Found Solution:编辑找到的解决方案:

Found the error in my code.在我的代码中发现了错误。 I'm running this on a unit test and Android doesn't use JSON objects on unit tests because it's part of android.我在单元测试中运行它,Android 在单元测试中不使用 JSON 对象,因为它是 android 的一部分。 That's why it was returning null.这就是它返回 null 的原因。

Question:题:

I'm tying to convert my String back to a JsonObject using JSONObject("string")我正在尝试使用 JSONObject("string") 将我的 String 转换回 JsonObject

Here is a sample of my String:这是我的字符串示例:

{
  "sessions": [
    {
      "locations": [
        {
          "lat": "14.2294625",
          "lng": "121.1509005",
          "time": 1560262643000,
          "speed": 0,
          "speedLimit": 0
        },
        {
          "lat": "14.2294576",
          "lng": "121.1509498",
          "time": 1560262713000,
          "speed": 0,
          "speedLimit": 0
        },
        {
          "lat": "14.2294576",
          "lng": "121.1509498",
          "time": 1560262714000,
          "speed": 0,
          "speedLimit": 0
        }
      ],
      "name": "1.5645220491E12"
    }
  ]
}

Its returning null on my JsonObjects:它在我的 JsonObjects 上返回 null:

content = "the string above"

var obj = JSONObject(content.substring(content.indexOf("{"), content.lastIndexOf("}") + 1))
System.out.println("obj1: " + obj.toString())

obj = JSONObject(content)
System.out.println("obj1: " + obj.toString())

var obj1 = JSONArray(content)
System.out.println("obj1: " + obj1.toString())

obj1 = JSONArray(content.substring(content.indexOf("{"), content.lastIndexOf("}") + 1))
System.out.println("obj2: " + obj1.toString())

All the outputs of this are null.这个的所有输出都是空的。 Any way to know what error happens so I can adjust my json string?有什么方法可以知道发生了什么错误,以便我可以调整我的 json 字符串?

  1. There is no need to substring your JSON string.无需对 JSON 字符串进行substring字符串化。 Put it inside JSONObject and it will work fine.将它放在JSONObject ,它会正常工作。
  2. If you need to get neasted objects use getJSONObject or getJSONArray methods如果您需要获取嵌套对象,请使用getJSONObjectgetJSONArray方法
  3. Show your content string in code (or how do you load it)在代码中显示您的content字符串(或如何加载它)

Your edited code您编辑的代码

val content = "the string above"

var obj = JSONObject(content)

I run this funtion to parse your string and it's ok.我运行这个函数来解析你的字符串,没关系。 Your string is valid.您的字符串有效。 Can you specify how do you init val content你能指定你如何初始化val content

 fun parseJson() {
    var stringJson = "{\"sessions\": [{\"locations\": [{\"lat\": \"14.2294625\",\"lng\": \"121.1509005\",\"time\": 1560262643000,\"speed\": 0,\"speedLimit\": 0},{\"lat\": \"14.2294576\",\"lng\": \"121.1509498\",\"time\": 1560262713000,\"speed\": 0,\"speedLimit\": 0},{\"lat\": \"14.2294576\",\"lng\": \"121.1509498\",\"time\": 1560262714000,\"speed\": 0,\"speedLimit\": 0}],\"name\": \"1.5645220491E12\"}  ]}"

    var obj = JSONObject(stringJson)
    System.out.println("obj1: $obj")
    var sessionArray: JSONArray = obj.optJSONArray("sessions")
    System.out.println("obj1: $sessionArray")
    var firstObject = sessionArray[0]
    System.out.println("obj1: $firstObject")
}

Since you are running your code with Junit, it running on the SDK on your computer.由于您使用 Junit 运行您的代码,因此它运行在您计算机上的 SDK 上。 This SDK doesn't provide everything, some are just some skeleton class providing signature of method and documentation but not the code.这个 SDK 并没有提供一切,有些只是一些提供方法和文档签名的骨架类,而不是代码。 So you can't execute directly.所以不能直接执行。

Try to add the library in testing尝试在测试中添加库

testImplementation 'org.json:json:your_version'

See the version here :在此处查看版本:

http://mvnrepository.com/artifact/org.json/json http://mvnrepository.com/artifact/org.json/json

This is based on the answer on this post: JSONObject returns a non null value of "null" after instantiating with string这是基于这篇文章的答案: JSONObject 在用字符串实例化后返回“null”的非空值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM