android客户端把一个字符串变成键值对(JSON之类的)的形式,比如:{name=张三,age=20,sex=男}。。实在不明白。。唉。。求解答。。
String[] arrays = new String[]{"name=张三", "age=20", "sex=男"};
JSONObject element = new JSONObject();
for(int i= 0; i< arrays.length; i++){
String str = arrays[i];
String[] strings = str.split("\=");
if(strings.length == 2){
try {
element.put(strings[0], strings[1]);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
建议你看一下jsoup很方便的
你可以把它先存入map 里边,之后把它tojson就可以了
二楼正解,自己在代码里拼接json很头晕的,可以使用Map存放, 再使用Gson的将map转化为json字符窜的方法
String[] arrays = new String[]{"name=张三", "age=20", "sex=男"};
JSONObject element = new JSONObject();
for(int i= 0; i< arrays.length; i++){
String str = arrays[i];
String[] strings = str.split("\=");
if(strings.length == 2){
try {
element.put(strings[0], strings[1]);
} catch (JSONException e) {
e.printStackTrace();
}
}
}