将从JDBC返回的Java String转换为Json对象

时间:2016-07-19 13:33:56

标签: java json

伟大的天才人,我需要帮助解决与java相关的java问题。在一个程序中,我正在进行JDBC调用,该函数返回一个类似于此的字符串:

[[{PROD_CD=42, SHORT_DESC=WATERFALL EDGE}, {PROD_CD=31, SHORT_DESC=N/A}, {PROD_CD=51, SHORT_DESC=OGEE EDGE}]]

我需要摆脱花括号,逗号,并将其保存为json对象。

[
  {
    "PROD_CD": " 42",
    "SHORT_DESC": "WATERFALL EDGE",
  },
  {
    "PROD_CD": "31",
    "SHORT_DESC": "N/A",
  },
  {
    "PROD_CD": "51",
    "SHORT_DESC": "OGEE EDGE",
  }
]

我非常感谢您的帮助

这是我到目前为止所尝试的内容:

@Override
    public Map<String, String> getEdgeCd() {


        Map<String, String> EdgeCd = new HashMap<String, String>();
        Map<String,Object> temp = new HashMap<String,Object>();


        try {


            SimpleJdbcCall fgetEdgeCd = new SimpleJdbcCall(jdbcTemplate)
                    .withSchemaName("logprd")
                    .withCatalogName("edge_api_pkg")
                    .withFunctionName("DDGetEdgeCd");


            temp = fgetEdgeCd.execute();
            System.out.println("temp " + temp + "  \n\n\n\n\n\n");
            System.out.println("temp.values() " + temp.values() + " lines \n\n\n\n\n\n");

            String keyList =  temp.values().toString();

//这是以下列字符串返回:

String keyList =  "[[{PROD_CD=42, SHORT_DESC=WATERFALL EDGE}, {PROD_CD=31, SHORT_DESC=N/A}, {PROD_CD=51, SHORT_DESC=OGEE EDGE}]]"; 
String[] currentLine;

currentLine = keyList.substring(3, keyList.length() -3).split("[}]|[{]");
String currenLineString = Arrays.toString(currentLine);

String newCurrentLineString = currenLineString.substring(1, keyList.length()-1).replaceAll("," , "").replaceAll(" EDGE" , "-Edge").replaceAll("\\s+", " ");
//System.out.println("newCurrentLineString:>"+ newCurrentLineString + "\n\n");
String[] testLine;
testLine = newCurrentLineString.split(" ");


ArrayList<LinkedHashMap<String, Object>> data = new ArrayList<LinkedHashMap<String, Object>>();
LinkedHashMap<String, Object> map=new LinkedHashMap<String, Object>();
Collection<String> keyValue = null;
    for(int i=0; i< testLine.length; i++) {
     String[] temp = testLine[i].toString().split("=");
    keyValue.addAll(Arrays.asList(temp));
    System.out.println( "keyValue"+i + ":>"+ keyValue.toString() + "\n\n");

    for (int j=0; j < keyValue.size(); j+=2) {
     map.put(temp[j].toString(), temp[j+1].toString());
     //map.put(keyValue[i].toString(), keyValue[i+1].toString());
     //System.out.println( "mapmain:>"+ map.toString() + "\n\n");
     data.add(map);
     System.out.println(map.toString());
     System.out.println(data.toString());

    }//end for j
   }end for i
      } catch (Exception e) {

            logger.error("Error trying JDBC");
    }

        return EdgeCd;
    }

}

2 个答案:

答案 0 :(得分:0)

这是一个答案, 但不一定是你想要的答案。

  1. 查看从数据库返回的数据。请注意,它有一个明显的模式。
  2. 数据库中有三个基本对象从数据库返回;数组,容器对象,键值对。该数组是容器对象的数组。容器对象包含一个或多个键值对。键值对由两个String值组成。
  3. 创建一个解析器,用于解析从数据库返回的数据。我建议不要试图&#34;修复&#34;它使用正则表达式,只需用代码解析它。
  4. 使用上述三个对象,使用解析器创建数据的对象表示(也在上面提到)。
  5. 将对象输出为json。

答案 1 :(得分:0)

这是C#中的一个快速功能,可以轻松移植到JAVA(抱歉,我的手机上没有JAVA编译器)。如果大括号或逗号出现在记录数据中,主要问题将是假设空格和转义字符。

        string data = "[[{PROD_CD=42, SHORT_DESC=WATERFALL EDGE}, {PROD_CD=31, SHORT_DESC=N/A}, {PROD_CD=51, SHORT_DESC=OGEE EDGE}]]";

        char[] chars = data.ToCharArray();
        StringBuilder currentRecord = null;
        StringBuilder json = new StringBuilder("[");

        bool isInCurly = false;// Loop state 
        for (int i=0;i<chars.Length; ++i)
        {
            if (chars[i] == '{')
            {
                isInCurly = true;
                currentRecord = new StringBuilder("{");
            }
            else if (chars[i] == '}')
            {
                currentRecord.Append("}");
                isInCurly = false;

                // Major assumptions made here about the structure that will need to be verified such as ", " between record values, etc...
                string cleanRecord = currentRecord.ToString().Replace("{", "{\"")
                                                             .Replace("=", "\":\"")
                                                             .Replace(", ", "\", \"")
                                                             .Replace("}", "\"}");
                json.AppendLine(cleanRecord + ", ");
            }
            else if(isInCurly)
            {
                currentRecord.Append(chars[i]);
            }
        }            
        json.Append("]");
        string finalJson = json.ToString();

结果 enter image description here