简体   繁体   中英

how to get values in JSON object and JSON array using java

I need to return a string in JSON to combine of json object and json array this is the example i need

 {

  "scode" : "62573000", "sname" : "Burn of right",
  "icd10" = [
    {"icode" : "T25.229?", "iname" : "Right foot"}
    {"icode" : "T25.22941?", "iname" : "left foot"}
  ],
  "refinement" = [
    {"rname" : "Refinement1"},
    {"rname" : "Refinement2"}
  ]
}

but i getting like this

{
    "icdcode": "T25.229?",
    "snomedcode": "62537000",
    "snomedname": "Second degree burn of foot (disorder)",
    "icdname": "Burn of second degree of unspecified foot, episode of care unspecified"
} 

this is the code

oJsonAry = new JSONArray();

        while (resultSet.next())
        {           
            JSONObject oJsonOther = new JSONObject();
            JSONObject oJsonRefine = new JSONObject();
            hMapotherwise = new HashMap<String, String>();

            maprule = (resultSet.getString("mapRule"));
            if (maprule.matches("OTHERWISE TRUE")|| maprule.matches("TRUE"))
            {
                strSnomedCode=resultSet.getString("referencedComponentId");
                hMapotherwise.put("snomedcode", strSnomedCode);

                strSnomedDesc=resultSet.getString("sctName");
                hMapotherwise.put("snomedname", strSnomedDesc);

                strIcdCode=resultSet.getString("mapTarget");
                hMapotherwise.put("icdcode", strIcdCode);

                strIcdName=resultSet.getString("icdName");
                hMapotherwise.put("icdname", strIcdName);

                oJsonOther.putAll(hMapotherwise);
                oJsonAry.add(oJsonOther);
            }
                refid = resultSet.getInt("refid");
                pipe = resultSet.getString("mapRule").split("\\|");

                if (pipe.length > 1)
                {
                    bSubmit = true;
                    oJsonRefine.put("maprule", pipe);

                    oJsonAry.add(oJsonRefine);
                }


                if(oJsonAry != null) 
                {
                    strJSON = oJsonAry.toJSONString();
                }
        }
        }

what are the things which i need to change in my java code and i new to this so that i get stuck in this.could any one say how to overcome from this .finally i have to return in strJSON

Your code doesn't make sense. Please try to be clearer in the next time. First your Json is not in correct syntax. The correct should be:

{
    "scode": "62573000",
    "sname": "Burn of right",
    "icd10": [{
        "icode": "T25.229?",
        "iname": "Right foot"
    }, {
        "icode": "T25.22941?",
        "iname": "left foot"
    }],
    "refinement": [{
        "rname": "Refinement1"
    }, {
        "rname": "Refinement2"
    }]
}

Second you want one type of return but you are using different variable names in your return example. ( We have to guess ).

With all these problems in your question, I tried to solve your problem, so lets see. You need to declare your arrays out side of your while.

        // Declare your arrays and main json outside of the while
    JSONArray arrayJsonIcd10 = new JSONArray();
    JSONArray arrayJsonRefinement = new JSONArray();
    JSONObject mainJsonObject = new JSONObject();

    while (resultSet.next())
    {           

        maprule = (resultSet.getString("mapRule"));
        if (maprule.matches("OTHERWISE TRUE")|| maprule.matches("TRUE"))
        {

            // create two objects inside the while to save your
            //objects that you need in the array
            JSONObject jsonObjectToScode = new JSONObject();
            JSONObject jsonObjectToRefinment = new JSONObject();

            strSnomedCode = resultSet.getString("referencedComponentId");
            mainJsonObject.put("snomedcode", strSnomedCode);

            strSnomedDesc = resultSet.getString("sctName");
            mainJsonObject.put("snomedname", strSnomedDesc);

            strIcdCode = resultSet.getString("mapTarget");
            jsonObjectToScode.put("icdcode", strIcdCode);

            strIcdName = resultSet.getString("icdName");
            jsonObjectToScode.put("icdName", strIcdName);

            arrayJsonIcd10.put(jsonObjectToScode);
        }
        refid = resultSet.getInt("refid");
        pipe = resultSet.getString("mapRule").split("\\|");

        if (pipe.length > 1)
        {
            bSubmit = true;
            jsonObjectToRefinment.put("rname", pipe);
            arrayJsonRefinement.put(jsonObjectToRefinment);

        }



    }
    mainJsonObject.put("icd10", arrayJsonIcd10 );
    mainJsonObject.put("refinement", arrayJsonRefinement);
    strJSON = mainJsonObject.toString();    

}

That's it but I am still confused how do you wanna obtain ("scode" : "62573000", "sname" : "Burn of right") if the values are inside the while. I guess you'll need a other array to save this values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM