我正在解析json文件并将架构信息添加到嵌套的哈希图中,但是当我尝试打印嵌套的哈希图时,它为我提供了所有键的相同值。需要帮助如何正确将数据存储到嵌套的哈希图中。

我的json文件:

{
  "status":"success",
  "tables":[
      {
         "dbname":"idn",
        "tableName":"my_monthly_hits_b",
        "schema":"(cm11:chararray)",
        "location":"/user/mydb/"
      },
      {
         "dbname":"idn",
         "tableName": "my_monthly_match",
         "schema":"(city:chararray,match:chararray,cm11:chararray)",
         "location":"/user/mydb1"
      }
   ]
}


我的代码:

public Map<String,Map<String,String>> getDataTypes(String responsePath){
  Map<String,Map<String,String>> maped = new HashMap<String,Map<String,String>>();
Map<String,String> colDataTypes = new HashMap<String,String>();
try{
JsonParser parser = new JsonParser();
Object obj = parser.parse(new FileReader(responsePath);
JsonObject jObj = (JsonObject) obj;
JsonArray jArray = (JsonArray) jObj.get("tables");
Iterator<JsonElement> itr = jArray.iterator();

while(itr.hasNext())
{
    JsonObject  innerObj = (JsonObject) itr.next();
    JsonElement shm = innerObj.get("schema");
    JsonElement jTableName = innerObj.get("tableName");
    String tableName = jTableName.toString();
    String ss = shm.toString().replaceAll("\"","").replaceAll("[()]",""):
    System.out.println("The required JSON string --->" + ss);
    if(ss.contains(","){
       String[] str = ss.split(",");
       for(String s: str){
          String[] ptr = s.split(":");
          colDataTypes.put(prt[0],ptr[1]);
       }
   }
   else{
       String[] str1 = ss.split(":");
       colDataTypes.put(str1[0],str1[1]);
  }
  maped.put(tabName,colDataTypes);
  for(String tab : maped.keySet()){
     System.out.println("#####" + "Table Name " + tab + "value" + maped.get(tab));
}
}
}
catch(FileNotFoundException ex)
{
}
return maped;
}

最佳答案

您可以使用Jackson之类的库来操作JSON树(如下所示)或将JSON编组为对象图。

package foo;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

public class Foo {

    public static void main(String[] args) throws Exception {
        final ObjectMapper mapper = new ObjectMapper();
        final JsonNode node = mapper.readTree("{  " +
                "   \"status\":\"success\"," +
                "   \"tables\":[  " +
                "      {  " +
                "         \"dbname\":\"idn\"," +
                "         \"tableName\":\"my_monthly_hits_b\"," +
                "         \"schema\":\"(cm11:chararray)\"," +
                "         \"location\":\"/user/mydb/\"" +
                "      }," +
                "      {  " +
                "         \"dbname\":\"idn\"," +
                "         \"tableName\":\"my_monthly_match\"," +
                "         \"schema\":\"(city:chararray,match:chararray,cm11:chararray)\"," +
                "         \"location\":\"/user/mydb1\"" +
                "      }" +
                "   ]" +
                "}");

        final ArrayNode tables = (ArrayNode) node.get("tables");

        // Add a new schema
        final ObjectNode newSchema = tables.addObject();

        newSchema.put("dbname", "foo db name");
        newSchema.put("tableName", "foo table name");
        newSchema.put("schema", "(foo:chararray,bar:chararray)");
        newSchema.put("location", "/foo/bar");

        mapper.enable(SerializationFeature.INDENT_OUTPUT);
        System.out.println(mapper.writeValueAsString(node));
    }
}


上面的示例将打印:

{
  "status" : "success",
  "tables" : [ {
    "dbname" : "idn",
    "tableName" : "my_monthly_hits_b",
    "schema" : "(cm11:chararray)",
    "location" : "/user/mydb/"
  }, {
    "dbname" : "idn",
    "tableName" : "my_monthly_match",
    "schema" : "(city:chararray,match:chararray,cm11:chararray)",
    "location" : "/user/mydb1"
  }, {
    "dbname" : "foo db name",
    "tableName" : "foo table name",
    "schema" : "(foo:chararray,bar:chararray)",
    "location" : "/foo/bar"
  } ]
}

关于java - 如何正确将数据添加到嵌套的哈希图中,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29684696/

10-09 01:11