gson 解析双引号上的Json时出现问题

pdsfdshx  于 2022-11-06  发布在  其他
关注(0)|答案(2)|浏览(323)

我正在使用Gson将json转换为java对象。

  1. {
  2. "assetClassDetails":[{}],
  3. "assetClassRequired":null,
  4. "baseUnitofMeasure":"PMI",
  5. "bomParent":"Yes",
  6. "commodityCodeTaric":"84158200",
  7. "enLanguageKey":"EN",
  8. "enMaterialLongText":"Lenovo Privacy Filter for 14" Notebooks ( L, T and X1 Carbon)"",
  9. "grossWeightInKg":null,
  10. "height":null,
  11. "heightLengthWidthUnit":null,
  12. "length":null,"manufacturerPartNumber":"",
  13. "materialLongDescription":"Lenovo Privacy Filter for 14" Notebooks ( L, T and X1 Carbon)"",
  14. "physicalCategory":"Physical",
  15. "volume":null,
  16. "volumeUnit":null,
  17. "width":null,
  18. "xxLanguageKey":null,
  19. "xxMaterialLongText":null
  20. }

问题出在属性enMaterialLongText和materialLongDescription上。在它们的值中有引号。Gson无法解析json。我的代码是

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

OperationalInfo类是这样的:

  1. public class OperationalInfo {
  2. private String commodityCodeTaric= null; // fETCH FROM db
  3. private String physicalCategory= null;
  4. private String materialType= null; // ZN01 *
  5. private String productHierarchy= null; // Gnp Hierarchy
  6. private String baseUnitofMeasure= null; // From Database
  7. private String height= null; // Manual enetered
  8. private String length= null; // Manual enetered
  9. private String width= null; // Manual enetered
  10. private String heightLengthWidthUnit= null; // Manual enetered
  11. private String volume= null; // Manual enetered
  12. private String volumeUnit= null; // Manual enetered
  13. private String grossWeightInKg= null; // Manual enetered
  14. private String itemCategoryGroup= null;
  15. private String manufacturerPartNumber= null; // is same as spn
  16. private String bomParent= null; // yes/No
  17. private String materialLongDescription= null; // Manual enetered
  18. private String enLanguageKey= null; // EN
  19. private String enMaterialLongText= null; // materialLongDescription
  20. private String xxLanguageKey= null; // EN
  21. private String xxMaterialLongText= null;
  22. private String catalogueGroup = null;
  23. private String pirStatus = null; // Changed, Add , mark for Delete
  24. private List<AssetClassLedger> assetClassDetails = null;
  25. private Boolean assetClassRequired = null;
  26. ...
  27. }

我已经尝试在输入json上使用String.replace()将双引号替换为“"。

**更新:**请参考我的以下代码:

  1. Gson gson = new Gson();
  2. 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\":\"\"}";
  3. JsonReader reader1 = new JsonReader(new StringReader(test));
  4. reader1.setLenient(true);
  5. OperationalInfo operationalInfo = gson.fromJson(reader1, OperationalInfo.class);

请注意,我已经为14英寸的enMaterialLongText和materialLongDescription使用了3个反斜杠。但在编程中,我如何知道哪一个引号要用三个反斜杠转义,哪一个引号要用一个反斜杠转义?

cigdeys3

cigdeys31#

由于您的JSON是无效的,我会联系JSON的源以获得有效的JSON。但是如果您真的没有其他方法获得有效的JSON,您可以尝试事先编辑JSON以使其近似有效。但是由于您无法正确地知道JSON的外观,我强烈建议您不要使用此方法。

  1. /**
  2. * This method tries to escape incorrect quotes. To do this, it looks at which character comes
  3. * after a quote character, because it assumes that a closed quote character is followed by either
  4. * a ",", ":", or "\n". However, if none of the characters follows, the quotation mark is
  5. * escaped.
  6. * <br />
  7. * Example Input:
  8. * <pre>
  9. * { "hello": "world " test "", "number": 0 }
  10. * </pre>
  11. * Example Output:
  12. * <pre>
  13. * { "hello": "world \" test \"", "number": 0 }
  14. * </pre>
  15. *
  16. * @param json The invalid JSON input
  17. * @return (Hopefully) correct JSON
  18. */
  19. private static String tryFixJson(String json) {
  20. final StringBuilder resp = new StringBuilder();
  21. final char[] chars = json.toCharArray();
  22. boolean open = false;
  23. for (int i = 0; i < chars.length; i++) {
  24. final char c = chars[i];
  25. if (c == '"') {
  26. if (!open) {
  27. if (i != 0 && chars[i - 1] != '\\') {
  28. open = true;
  29. }
  30. } else if (i != chars.length - 1) {
  31. if (chars[i - 1] != '\\') {
  32. final char n = chars[i + 1];
  33. if (n != ',' && n != ':' && n != '\n') {
  34. resp.append('\\');
  35. } else {
  36. open = false;
  37. }
  38. }
  39. }
  40. }
  41. resp.append(c);
  42. }
  43. return resp.toString();
  44. }
展开查看全部
jei2mxaa

jei2mxaa2#

它失败的原因是需要对特殊字符(如"\)进行转义。只需在特殊字符前添加一个反斜杠,就可以了:

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

materialLongDescription也是如此。

相关问题