簡體   English   中英

解析Json雙引號的問題

[英]Problem with parsing Json double-quotation mark

我正在使用 Gson 將 json 轉換為 java ZA8CFDE6331BD59EB2AC96F8911C4B6 這是 Gson

{
    "assetClassDetails":[{}],
    "assetClassRequired":null,
    "baseUnitofMeasure":"PMI",
    "bomParent":"Yes",
    "commodityCodeTaric":"84158200",
    "enLanguageKey":"EN",
    "enMaterialLongText":"Lenovo Privacy Filter for 14" Notebooks ( L, T and X1 Carbon)"",
    "grossWeightInKg":null,
    "height":null,
    "heightLengthWidthUnit":null,
    "length":null,"manufacturerPartNumber":"",
    "materialLongDescription":"Lenovo Privacy Filter for 14" Notebooks ( L, T and X1 Carbon)"",
    "physicalCategory":"Physical",
    "volume":null,
    "volumeUnit":null,
    "width":null,
    "xxLanguageKey":null,
    "xxMaterialLongText":null
 }

問題在於屬性 enMaterialLongText 和 materialLongDescription。 內部值有引號。Gson 無法解析 json。 我的代碼是

Gson gson = new Gson();
OperationalInfo outout = gson.fromJson(inputJson, OperationalInfo.class);

OperationalInfo class 是這樣的:

public class OperationalInfo {

private String commodityCodeTaric= null;  //  fETCH FROM db

private String physicalCategory= null; 
private String materialType= null; // ZN01 *
private String productHierarchy= null; // Gnp Hierarchy
private String baseUnitofMeasure= null; // From Database
private String height= null; // Manual enetered
private String length= null; // Manual enetered
private String width= null; // Manual enetered
private String heightLengthWidthUnit= null; // Manual enetered
private String volume= null; // Manual enetered
private String volumeUnit= null; // Manual enetered
private String grossWeightInKg= null; // Manual enetered
private String itemCategoryGroup= null;
private String manufacturerPartNumber= null; // is same as spn
private String bomParent= null;  // yes/No    
private String materialLongDescription= null; // Manual enetered
private String enLanguageKey= null; // EN
private String enMaterialLongText= null; // materialLongDescription
private String xxLanguageKey= null;   //  EN
private String xxMaterialLongText= null;      
private String catalogueGroup = null;
private String pirStatus = null;  // Changed, Add , mark for Delete
private List<AssetClassLedger> assetClassDetails = null;
private Boolean assetClassRequired = null;
...
}

我已嘗試在輸入 json 上使用 String.replace() 將雙引號替換為“\”。

更新:請參考我的以下代碼:

Gson gson = new Gson();
String test = "{\"assetClassRequired\":\"\",\"baseUnitofMeasure\":\"PMI\",\"bomParent\":\"Yes\",\"commodityCodeTaric\":\"84158200\",\"enLanguageKey\":\"EN\",\"enMaterialLongText\":\"Lenovo Privacy Filter for 14\\\" Notebooks ( L, T and X1 Carbon)\\\"\",\"grossWeightInKg\":\"\",\"height\":\"\",\"heightLengthWidthUnit\":\"\",\"length\":\"\",\"manufacturerPartNumber\":\"\",\"materialLongDescription\":\"Lenovo Privacy Filter for 14\\\" Notebooks ( L, T and X1 Carbon)\\\"\",\"physicalCategory\":\"Physical\",\"volume\":\"\",\"volumeUnit\":\"\",\"width\":\"\",\"xxLanguageKey\":\"\",\"xxMaterialLongText\":\"\"}";
JsonReader reader1 = new JsonReader(new StringReader(test));
reader1.setLenient(true);
OperationalInfo operationalInfo = gson.fromJson(reader1, OperationalInfo.class);

請看到我為 enMaterialLongText 和 14 英寸的 materialLongDescription 使用了 3 個反斜杠。 但是以編程方式,我如何知道要通過三反斜杠轉義哪個引號以及要單反斜杠轉義哪個引號?

由於您的 JSON 無效,我將聯系 JSON 的來源以獲得有效的 JSON。 但是如果你真的沒有其他方法可以獲得有效的 JSON,你可以嘗試事先編輯 JSON 使其大致有效。 但是由於您無法正確了解 JSON 的外觀,因此我強烈建議您不要使用此方法。

/**
 * This method tries to escape incorrect quotes. To do this, it looks at which character comes
 * after a quote character, because it assumes that a closed quote character is followed by either
 * a ",", ":", or "\n". However, if none of the characters follows, the quotation mark is
 * escaped.
 * <br />
 * Example Input:
 * <pre>
 *   { "hello": "world " test "", "number": 0 }
 * </pre>
 * Example Output:
 * <pre>
 *   { "hello": "world \" test \"", "number": 0 }
 * </pre>
 *
 * @param json The invalid JSON input
 * @return (Hopefully) correct JSON
 */
private static String tryFixJson(String json) {
  final StringBuilder resp = new StringBuilder();
  final char[] chars = json.toCharArray();
  boolean open = false;
  for (int i = 0; i < chars.length; i++) {
    final char c = chars[i];
    if (c == '"') {
      if (!open) {
        if (i != 0 && chars[i - 1] != '\\') {
          open = true;
        }
      } else if (i != chars.length - 1) {
        if (chars[i - 1] != '\\') {
          final char n = chars[i + 1];
          if (n != ',' && n != ':' && n != '\n') {
            resp.append('\\');
          } else {
            open = false;
          }
        }
      }
    }
    resp.append(c);
  }
  return resp.toString();
}

它失敗了,因為您需要轉義特殊字符,例如"\ 。只需在特殊字符前添加一個反斜杠就可以了:

{
  "enMaterialLongText": "Lenovo Privacy Filter for 14\" Notebooks ( L, T and X1 Carbon)\""
}

materialLongDescription也是如此。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM