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

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

How to write Java Array string to Json File

问题

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

String Stringtojson = Concstring;

JSONParser parser = new JSONParser();
try {
    Object object = parser.parse(Stringtojson);
    
    // 将 Object 转换为 JSONObject
    JSONObject jsonObject3 = (JSONObject) object;
    try {
        FileWriter file = new FileWriter("F:\\myjson.json");
        file.write(jsonObject3.toJSONString());
        file.close();
    } catch (IOException e) {
        e.printStackTrace();
    }

} catch (Exception e) {
    e.printStackTrace();
}

请注意,由于您要求只返回翻译好的部分,我已将翻译结果放在代码块中,原始文本中的 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:

[
      { "foedselsdato" : "2012-04-29",
        "individId" : "052804211084",
        "undergruppe" : "KU",
        "varslinger" :
        { "medisinering" :
          [
            { "merknad" : "",
              "preparat" : "11 Mastipen vet Intramammarie 300 mg\/sprøyte",
              "tilbakeholdelsesdato" : "2020-08-25" },

            { "merknad" : "",
              "preparat" : "11 Penovet vet Inj væske, susp 300 mg\/ml",
              "tilbakeholdelsesdato" : "2020-08-25" } ] } },

      { "foedselsdato" : "2017-05-19",
        "individId" : "052820651335",
        "undergruppe" : "KU",
        "varslinger" :
        { "helsetilstand" :
          [
            { "merknad" : "Ole test",
              "tilstand" : "SVAK_HALTHET",
              "tiltak" : "EKSTRA_STROE" } ] } } ]

Below is my code:

String Stringtojson =  Concstring;

JSONParser parser = new JSONParser();
try
{
    Object object = parser.parse(Stringtojson);
    
    //convert Object to JSONObject
    JSONObject jsonObject3 = (JSONObject)object;
    try {
        FileWriter file = new FileWriter("F:\myjson.json");
        file.write(jsonObject3.toJSONString());
        file.close();
        } catch (IOException e) {                                        
        e.printStackTrace();
        }
    

}
catch(Exception e)
{
    e.printStackTrace();
}

答案1

得分: 1

`Object object = parser.parse(Stringtojson);` 这行代码实际上将您的字符串转换为一个 `JSONArray`。原因是外部的数组括号(即"["和"]")。将 `JSONArray` 强制转换为 `JSONObject` 导致崩溃(`JSONObject jsonObject3 = (JSONObject)object;`)

您基本上有两个选项:
1) 将 `JSONObject jsonObject3 = (JSONObject)object;` 这行替换为 `JSONArray jsonObject3 = (JSONArray)object;`。然后文件的内容将完全是您的字符串(并且它将是一个有效的 JSON 文件)。

2) 将外部的 `[` 替换为 `{ "root": [`,将 `]` 替换为 `] }`。这将把您的 JSONArray 转换为 JSONObject。

总的来说,这涉及到 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

String Stringtojson =  Concstring;

JSONParser parser = new JSONParser();
try
{
    Object object = parser.parse(Stringtojson);

    //convert Object to JSONObject
    JSONArray jsonObject3 = (JSONArray)object;
    try {
        FileWriter file = new FileWriter(resultDir);
        file.write(jsonObject3.toString());
        file.close();
     } catch (IOException e) {                                        
        e.printStackTrace();
     }


}
catch(Exception e)
{
    e.printStackTrace();
}
英文:
String Stringtojson =  Concstring;

JSONParser parser = new JSONParser();
try
{
    Object object = parser.parse(Stringtojson);
    
    //convert Object to JSONObject
    JSONArray jsonObject3 = (JSONArray)object;
    try {
        FileWriter file = new FileWriter(resultDir);
        file.write(jsonObject3.toString());
        file.close();
     } catch (IOException e) {                                        
        e.printStackTrace();
     }
    

}
catch(Exception e)
{
    e.printStackTrace();
}

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:

确定