2

我正在尝试使用messagpack.write(map). 在使用messagepack.read(byte[])i got进行反序列化期间MapValue。但我无法使用MapValue.get(key). 看下面这个问题

  HashMap<Object,Object> map = new HashMap<Object, Object>();
  map.put(1,"ONE");
  map.put("ONE","TWO");
  MessagePack m= new MessagePack();
  byte[] b = m.write(map);
  MessagePack m1 = new MessagePack();
  MapValue value = (MapValue)m1.read(b);
  System.out.println(value);// here I am getting {1:"ONE",2:"TWO"}

 System.out.println( value.get(1)); // printing the value for key 1. I am getting null.

请对此提供帮助。谢谢。

瑙萨德

4

1 回答 1

3

您需要使用 ValueFactory 并转换键以使用 Value 接口。这不是很直观

// instead of value.get(1) use following
System.out.println(value.get(ValueFactory.createIntegerValue(1)));

// if the key would be a String use:
System.out.println(value.get(ValueFactory.createRawValue("key")));
于 2013-10-22T15:45:46.353 回答