【问题标题】:how to get values in JSON object and JSON array using java如何使用java获取JSON对象和JSON数组中的值
【发布时间】:2016-07-27 19:25:22
【问题描述】:

我需要在 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

【问题讨论】:

标签: java arrays json object jsonobject


【解决方案1】:

您的代码没有意义。请尝试在下一次更清楚。 首先,您的 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"
    }]
}

其次,您想要一种类型的返回,但您在返回示例中使用了不同的变量名称。 (我们必须猜测)。

由于您的问题中存在所有这些问题,我试图解决您的问题,所以让我们看看。您需要在 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();    

}

就是这样,但如果值在 while 内,我仍然很困惑你想如何获得(“scode”:“62573000”,“sname”:“Burn of right”)。我猜你需要另一个数组来保存这些值。

【讨论】:

  • 是的@tiagoribeirof 它工作正常。谢谢,我是 json 新手,这就是问题
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多