0

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();
}
                                
3
  • Because of the square brackets surrounding the JSON , the parser wants to turn the whole thing into an array. Try removing the outer array brackets Commented Oct 23, 2020 at 6:43
  • The outer bracket are needed in JSON File, its a format needed to submit to API Commented Oct 23, 2020 at 6:48
  • 1
    class org.json.simple.JSONArray cannot be cast to class org.json.simple.JSONObject already tells you what the problem is: you get a JSONArray which is perfectly fine for your json string but you can't cast that to JSONObject. So just cast to JSONArray instead. Commented Oct 23, 2020 at 6:48

2 Answers 2

1

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).

Sign up to request clarification or add additional context in comments.

2 Comments

Am trying to write the object to file , am a little bit stuck :(
Replace JSONObject jsonObject3 = (JSONObject)object; with JSONArray jsonObject3 = (JSONArray)object;. You might replace also jsonObject3.toJSONString() with jsonObject3.toString().(I am right now not sure which JSON library you use).
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();
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.