-2

我在值中有一个带有 JSON 的 Map.Entry,我想将此值设为 Map

使用示例将更容易解释:我正在向这样的 URL 发出 api 请求:

response1 = httpclient.execute(httpGet);

这是我从请求中得到的 JSON:

{"Projects":
  [{"Role":"ProjectAdmin","Url":"http://**/","OverSized":false,"Folders":      
     [{"Name":"sh","Url":"http://**/","Permssion":"rw"}]},
   {"Role":"ProjectAdmin","Url":"http://**/Goooood","OverSized":false,"Folders":     
     [{"Name":"Goooood","Url":"http://**/Goooood","Permssion":"rw"}]}],
 "DeviceToken":"token",
 "TotalProjects":2}

这就是我将得到的 JSON 转换为 MAP 的方式:

ContainerFactory containerFactory = new ContainerFactory()
{
  public List creatArrayContainer() {return new LinkedList();}
  public Map createObjectContainer() {return new LinkedHashMap();}
};
BufferedReader br = new BufferedReader(new InputStreamReader(response1.getEntity().getContent()));
StringBuilder content = new StringBuilder();
String line;
//TODO:Remove and build a better JSON parser
while (null != (line = br.readLine()))
{
   content.append(line);
}
JSONParser parser = new JSONParser();    
try
{
  json = (Map)parser.parse(content.toString(), containerFactory);
  Iterator iter = json.entrySet().iterator();
}

现在一切都很好!现在我可以像这样做一段时间iter

while(iter.hasNext())
{
   Map.Entry entry = (Map.Entry)iter.next();
   System.out.println(entry.getKey() + "=>" + entry.getValue());
}

现在我的问题是我有一个 entry.getValue()里面有json。

例子:

   [{"Role":"ProjectAdmin","Url":"http://**/","OverSized":false,"Folders":      
     [{"Name":"sh","Url":"http://**/","Permssion":"rw"}]},
   {"Role":"ProjectAdmin","Url":"http://**/Goooood","OverSized":false,"Folders":     
     [{"Name":"Goooood","Url":"http://**/Goooood","Permssion":"rw"}]}]

我需要像从响应中那样制作一个 Map 才能循环它。有没有办法做到这一点 ?有没有办法做到正确?

我不认为像我一开始那样将它变成一个字符串并重新映射它是一个问题,但我觉得有更好的方法。

谢谢!

4

1 回答 1

1

尝试根据您的对象映射此代码

 Iterator<Entry<Long, String>> it = 
   yourMap.entrySet().iterator();
    while (it.hasNext()) {
    Map.Entry<Long, String> pairs = (Map.Entry<Long, String>) it.next();
        bw.write(pairs.getValue());
    }

希望这会有所帮助。

于 2013-05-09T10:59:22.230 回答