我需要在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"}
]
}
但我变得像这样
{
"icdcode": "T25.229?",
"snomedcode": "62537000",
"snomedname": "Second degree burn of foot (disorder)",
"icdname": "Burn of second degree of unspecified foot, episode of care unspecified"
}
这是代码
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();
}
}
}
我需要在我的java代码中更改的内容是什么,我是新手,因此我会陷入困境。任何人都说如何克服这一点。最后我必须在strJSON中返回
答案 0 :(得分:0)
您的代码没有意义。请在下次尝试更清楚。 首先你的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"
}]
}
其次,您需要一种类型的返回,但在返回示例中使用的是不同的变量名称。 (我们必须猜测。)
在你的问题中存在所有这些问题,我试图解决你的问题,所以让我们看看。你需要在你的时间内声明你的阵列。
// 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();
}
那就是它,但我仍然感到困惑,你是怎么想获得的(&#34; scode&#34;:&#34; 62573000&#34;,&#34; sname&#34;:&#34 ;正确的烧伤&#34;)如果值在while内。我想你需要另一个数组来保存这个值。