执行存储在String中的函数。 Java的

时间:2016-11-26 18:23:59

标签: java json stringbuffer

我想改变" cadena.toString()"通过String的值并执行最后一行。

    Map<String, String> config = new HashMap<String, String>();
    JsonBuilderFactory factory = Json.createBuilderFactory(config);

    JsonObject value = null ;
    StringBuffer cadena = new StringBuffer("");

    for(Friendship f: listFriends){ 
          cadena.append( ".add( \"From: "+f.getUser_from().getLogin()+"\",\"To: "+ f.getUser_to().getLogin()+"\")");
     }

     value = factory.createObjectBuilder().cadena.toString().build();

可能吗?

非常感谢所有人,抱歉我的英语不好。

1 个答案:

答案 0 :(得分:1)

No. Your first clue should be that factory.createObjectBuilder().cadena (probably) won't even compile.

Why can't you do something like this?

(note: replace Builder with the correct object type of factory.createObjectBuilder() return value)

Builder b = factory.createObjectBuilder();
for(Friendship f: listFriends){ 
      b.add("From: "+f.getUser_from().getLogin()+", To: "+ f.getUser_to().getLogin());
}
value = b.build();