简体   繁体   English

Gson无法解析位于JsonArray中的json对象中的json数组字符串

[英]Gson is unable to parse a json array string located in an json object AS a JsonArray

Context : 内容:

I am trying to convert the following JSON into a JAVA POJO using gson 我正在尝试使用gson将以下JSON转换为JAVA POJO

JSON : JSON:

    {
    "task": "findRecords",
    "foundRecords": "[1234567, 11234512]",
    "logs": "records found [1234567, 11234512]",
    "status": "success"
}

POJO Class : POJO类别:

public class JavaPojo {
  String task = null;
  JsonArray foundRecords = null;
  String logs = null;
  String status = null;
}

Conversion logic : 转换逻辑:

  JavaPojo pojo = gson.fromJson(jsonString, JavaPojo.class);

where jsonString is the string representation of the above mentioned json. 其中jsonString是上述json的字符串表示形式。

Problem : 问题:

The conversion logic fails with 转换逻辑失败

com.google.gson.JsonSyntaxException: Expected a com.google.gson.JsonArray but was com.google.gson.JsonPrimitive com.google.gson.JsonSyntaxException:预期为com.google.gson.JsonArray,但为com.google.gson.JsonPrimitive

From what i understand gson is interpreting foundRecords 's value as purely string and is not parsing it into an Array. 据我了解,gson会将foundRecords的值解释为纯字符串,而不是将其解析为Array。

Now to get around this i modified foundRecords from JsonArray to String . 现在要解决此问题,我将foundRecordsJsonArray修改为String the conversion happened successfully. 转换成功完成。

Any clue how can i achieve the conversion while preserving the type of foundRecords as JsonArray ? 有什么线索可以在将foundRecords类型保留为JsonArray同时实现转换吗?

Note: 注意:

After converting foundRecords to String , i tried converting the string value of foundRecords to JsonArray using gson and it strangely worked. 转换后foundRecordsString ,我试过的字符串值转换foundRecordsJsonArray使用GSON而且奇怪的工作。

//This works
//Map json to pojo
  JavaPojo pojo = gson.fromJson(jsonString, JavaPojo.class);
//Convert foundRecords into a JsonArray
JsonArray arr = gson.fromJson(pojo.getFoundRecords(), JsonArray.class);

So now i am confused, if gson can convert string type foundRecords into JsonArray then why couldn't it perform the conversion earlier ? 因此,现在我很困惑,如果gson可以将foundFounds的string类型foundRecordsJsonArray那么为什么它不能更早地执行转换呢?

I understand the REST API response is bad and is violating the JSON syntax. 我了解REST API响应不好,并且违反了JSON语法。 Solution is to correct the REST API, but in my scenario unfotunately i cannot request for API correction so i wrote a util at my end to clean the jsonString . 解决方案是纠正REST API,但不幸的是,在我的情况下,我无法请求API纠正,因此我在结束时编写了一个util来清理jsonString

Posting it here if it helps anyone : 如果可以帮助任何人,请在此处发布:

  /**
   * @param malformedArrayKey
   *          - Name of the key in the JSON object that has a malformed array
   *          for e.g consider following JSON object having a bad formed array
   *          <pre>
   *      {
   *        "task": "findRecords",
   *        "foundRecords": "[1234567, 11234512]",
   *      }
   *          </pre>
   * @param jsonString
   *          - String representation of the JSON object containing the malformed array
   * @return - json string having well formed array against the key {@code malformedArrayKey} supplied
   *         <pre>
   *        {
   *        "task": "findRecords",
   *        "foundRecords": [1234567, 11234512]
   *        }
   *         </pre>
   */
  public static String formatMalformedArray(String malformedArrayKey, String jsonString) {
    JsonObject jsonObj = gson.fromJson(jsonString, JsonObject.class);
    // get the faulty key value
    String malformedArrayKeyValue = jsonObj.get(malformedArrayKey)
        .getAsString();
    // drop it
    jsonObj.remove(malformedArrayKey);
    // create a array out of the malformed array string
    JsonArray jsonArray = gson.fromJson(malformedArrayKeyValue, JsonArray.class);
    // add the array back to the object
    jsonObj.add(malformedArrayKey, jsonArray);
    // now convert it into a well formed json string
    return jsonObj.toString();
  }

The method is quite basic but it satisifes my use case. 该方法非常基本,但是可以满足我的用例。

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

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