java String转json,String转Map,Json转Map
Stringdata="{timestamp=1493114544899,body={"name":"\u6d4b\u8bd5\u5546\u6237","shop_id...
String data = "{timestamp=1493114544899, body={"name":"\u6d4b\u8bd5\u5546\u6237","shop_id":"123456"}, cmd=order.list, source=65504, ticket=C34A0D20-45EC-9C26-CAB8-3DA309213671, encrypt=des.v1, secret=123131243245454534, fields=a|b, version=3.0}";
Java里怎么将上面这串字符串转成Map类型?转成Json也行,最好能有实例。
JSONObject jsonObject = JSONObject.parseObject(data);这么转是不行的,中间是等号,不要来水经验 展开
Java里怎么将上面这串字符串转成Map类型?转成Json也行,最好能有实例。
JSONObject jsonObject = JSONObject.parseObject(data);这么转是不行的,中间是等号,不要来水经验 展开
1个回答
展开全部
用google的Gson类可以转换成Map类型,需要下载gson-2.3.1.jar,并且包含到你的build Path中,完整的Java程序如下
import java.util.HashMap;
import java.util.Map;
import com.google.gson.Gson;
public class H {
public static void main(String[] args) {
String data = "{timestamp=1493114544899, body={\"name\":\"\u6d4b\u8bd5\u5546\u6237\",\"shop_id\":\"123456\"}, cmd=order.list, source=65504, ticket=C34A0D20-45EC-9C26-CAB8-3DA309213671, encrypt=des.v1, secret=123131243245454534, fields=a|b, version=3.0}";
Gson gson = new Gson();
Map<String, Object> map = new HashMap<String, Object>();
map = gson.fromJson(data, map.getClass());
for (Map.Entry<String, Object> entry : map.entrySet()) {
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
}
}
运行结果
Key = ticket, Value = C34A0D20-45EC-9C26-CAB8-3DA309213671
Key = encrypt, Value = des.v1
Key = cmd, Value = order.list
Key = source, Value = 65504.0
Key = secret, Value = 1.23131243245454528E17
Key = body, Value = {name=测试商户, shop_id=123456}
Key = fields, Value = a|b
Key = version, Value = 3.0
Key = timestamp, Value = 1.493114544899E12
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询