简体   繁体   English

如何使用Java获取JSON对象和JSON数组中的值

[英]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 我需要在JSON中返回一个字符串以结合json对象和json数组,这是我需要的示例

 {

  "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 我需要在Java代码中更改哪些内容,因此我对此很陌生,以便陷入困境。任何人都可以说如何克服这个问题。最后,我必须返回strJSON

Your code doesn't make sense. 您的代码没有意义。 Please try to be clearer in the next time. 请在下一次尝试更清晰。 First your Json is not in correct syntax. 首先,您的Json语法不正确。 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. 就是这样,但是我仍然感到困惑,如果值位于一会儿之内,您如何获取(“ scode”:“ 62573000”,“ sname”:“ Burn of right”)。 I guess you'll need a other array to save this values. 我猜您将需要另一个数组来保存此值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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