如何将Java数组字符串写入Json文件

huangapple go评论91阅读模式
英文:

How to write Java Array string to Json File

问题

以下是您的代码翻译结果:

  1. String Stringtojson = Concstring;
  2. JSONParser parser = new JSONParser();
  3. try {
  4. Object object = parser.parse(Stringtojson);
  5. // 将 Object 转换为 JSONObject
  6. JSONObject jsonObject3 = (JSONObject) object;
  7. try {
  8. FileWriter file = new FileWriter("F:\\myjson.json");
  9. file.write(jsonObject3.toJSONString());
  10. file.close();
  11. } catch (IOException e) {
  12. e.printStackTrace();
  13. }
  14. } catch (Exception e) {
  15. e.printStackTrace();
  16. }

请注意,由于您要求只返回翻译好的部分,我已将翻译结果放在代码块中,原始文本中的 HTML 实体引用(如 ")已被正常翻译成双引号。

英文:

I have Java string and would like to parse it to JSON file. I first try to parse the string to an Object then to Json Object. The java string contains array/list of 2 elements. I would want to maintain that structure since its submitted to an API in that form.

Content of my String looks as below:

  1. [
  2. { "foedselsdato" : "2012-04-29",
  3. "individId" : "052804211084",
  4. "undergruppe" : "KU",
  5. "varslinger" :
  6. { "medisinering" :
  7. [
  8. { "merknad" : "",
  9. "preparat" : "11 Mastipen vet Intramammarie 300 mg\/sprøyte",
  10. "tilbakeholdelsesdato" : "2020-08-25" },
  11. { "merknad" : "",
  12. "preparat" : "11 Penovet vet Inj væske, susp 300 mg\/ml",
  13. "tilbakeholdelsesdato" : "2020-08-25" } ] } },
  14. { "foedselsdato" : "2017-05-19",
  15. "individId" : "052820651335",
  16. "undergruppe" : "KU",
  17. "varslinger" :
  18. { "helsetilstand" :
  19. [
  20. { "merknad" : "Ole test",
  21. "tilstand" : "SVAK_HALTHET",
  22. "tiltak" : "EKSTRA_STROE" } ] } } ]

Below is my code:

  1. String Stringtojson = Concstring;
  2. JSONParser parser = new JSONParser();
  3. try
  4. {
  5. Object object = parser.parse(Stringtojson);
  6. //convert Object to JSONObject
  7. JSONObject jsonObject3 = (JSONObject)object;
  8. try {
  9. FileWriter file = new FileWriter("F:\myjson.json");
  10. file.write(jsonObject3.toJSONString());
  11. file.close();
  12. } catch (IOException e) {
  13. e.printStackTrace();
  14. }
  15. }
  16. catch(Exception e)
  17. {
  18. e.printStackTrace();
  19. }

答案1

得分: 1

  1. `Object object = parser.parse(Stringtojson);` 这行代码实际上将您的字符串转换为一个 `JSONArray`。原因是外部的数组括号(即"["和"]")。将 `JSONArray` 强制转换为 `JSONObject` 导致崩溃(`JSONObject jsonObject3 = (JSONObject)object;`
  2. 您基本上有两个选项:
  3. 1) `JSONObject jsonObject3 = (JSONObject)object;` 这行替换为 `JSONArray jsonObject3 = (JSONArray)object;`。然后文件的内容将完全是您的字符串(并且它将是一个有效的 JSON 文件)。
  4. 2) 将外部的 `[` 替换为 `{ "root": [`,将 `]` 替换为 `] }`。这将把您的 JSONArray 转换为 JSONObject
  5. 总的来说,这涉及到 JSONObject "[]" 定义的差异(因为它本质上是元素列表),以及 JSONArray "{}" 定义的差异,它类似于字典(即您有键和值)。
英文:

The line Object object = parser.parse(Stringtojson); actually converts your string into an JSONArray. The reason are the outer array brackets (i.e. "[" and "]"). Casting a JSONArray to a JSONObject causes the crash (JSONObject jsonObject3 = (JSONObject)object;)

You have essentially two options:

  1. Replace the line JSONObject jsonObject3 = (JSONObject)object; with JSONArray jsonObject3 = (JSONArray)object;. Then the content of the file would be exactly your string (and it would be a valid JSON file).

  2. Replace the outter [ with { "root": [ and ] with ] }. This would turn your JSONArray into a JSONObject.

In general, this is all about the difference between a JSONObject which is defined by "[]" (as it is essentially a list of elements) and a JSONArray which is defined by "{}" and is comparable to a dictionary (i.e. you have keys and values).

答案2

得分: 0

  1. String Stringtojson = Concstring;
  2. JSONParser parser = new JSONParser();
  3. try
  4. {
  5. Object object = parser.parse(Stringtojson);
  6. //convert Object to JSONObject
  7. JSONArray jsonObject3 = (JSONArray)object;
  8. try {
  9. FileWriter file = new FileWriter(resultDir);
  10. file.write(jsonObject3.toString());
  11. file.close();
  12. } catch (IOException e) {
  13. e.printStackTrace();
  14. }
  15. }
  16. catch(Exception e)
  17. {
  18. e.printStackTrace();
  19. }
英文:
  1. String Stringtojson = Concstring;
  2. JSONParser parser = new JSONParser();
  3. try
  4. {
  5. Object object = parser.parse(Stringtojson);
  6. //convert Object to JSONObject
  7. JSONArray jsonObject3 = (JSONArray)object;
  8. try {
  9. FileWriter file = new FileWriter(resultDir);
  10. file.write(jsonObject3.toString());
  11. file.close();
  12. } catch (IOException e) {
  13. e.printStackTrace();
  14. }
  15. }
  16. catch(Exception e)
  17. {
  18. e.printStackTrace();
  19. }

huangapple
  • 本文由 发表于 2020年10月23日 14:38:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/64495073.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定