java json element,Gson,JsonElement,Java中的字符串比较

Ok, I'm wondering this could be quite simple and stupid but after a while fighting with the situation I have no idea what is happening.

I'm using Gson to handle a few JSON elements. Somewhere in my code I get one of the JsonElements of a JsonObject as a String and I compare it against another String. As far as I can see both of them are equals but when comparing I always get false. Here is the snippet.

JsonArray arr;

JsonObject jsonobj;

JsonElement model_elem;

String STUPID_STRING = "bla bla bla";

// Previously we initializes and fill arr, it doesn't matter how... I hope

jsonobj = arr.get(0).getAsJsonObject();

model_elem = jsonobj.get("coolname");

if (model_elem.toString().equals(STUPID_STRING)) {

...

It never goes inside the if statement.

arr has element at index 0, jsonobj has a field with name "coolname" and if I println model_elem i get "bla bla bla" (the same as STUPID_STRING). I have tried equals() and also compareTo() == 0.

I cannot figure out what is happening here, does anyone knows? :-s.

Thanks in advance.

解决方案

I believe you need to use getAsString() with GSON. toString() will add quotes!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值