![](/img/trans.png)
[英]ArrayList size 0 after deserialisation Android Studio Java
[英]JSON/Java/Android: deserialisation from a String to a ArrayList<Strings>
我正在寻找数小时将字符串反序列化为其先前的格式ArrayList。 毫无疑问,网络上有很多例子,但是我尝试的每个例子都至少缺少一个对象或命令。 因此,他们宣传了一种无法实现的方式。
该服务器具有以下实现:
for (String string:DBdata)
{ //put each string in DBdata to a JSON-Object with key=time and value=value
jSONString.put(string.substring(0, string.indexOf(",")), string.substring(string.indexOf(",")+1,string.indexOf(";")));
}
DBdata中的每个字符串都类似于123234:1234567; (UnixTimeOrIndex:Value;)如果反序列化后的输出为二维,也可以。
提前致谢。
我自己写了它:可能不是最有效的解决方案,但是它有效。主类中的所有内容都只是在组装一个字符串进行测试。
package test;
import java.util.ArrayList;
import net.sf.json.JSONObject;
public class Tst {
public static void main(String[] args) {
ArrayList<String> versuch=new ArrayList<String>();
for(int i=1; i<11; i++){
String temp = "Time1234"+i+",MeanValue123"+i+";";
System.out.println(temp);
versuch.add(temp);
}
System.out.println(versuch);
JSONObject jSONString = new JSONObject();
for (String string:versuch)
{ //put each string in DBdata to a JSON-Object with key=time and value=value
jSONString.put(string.substring(0, string.indexOf(",")), string.substring(string.indexOf(",")+1,string.indexOf(";")));
}
String output="data.ID=1234."+jSONString.toString();
System.out.println(output);
System.out.println(JSONDeconstruct(output));
}
public static ArrayList<String> JSONDeconstruct (String st)
{
ArrayList<String> out=new ArrayList<String>();
int begpos=st.indexOf("{");
int endpos=st.indexOf("}");
int index=0;
String work=st.substring(begpos+1, endpos);
String replaced=work.replace("\",\"", ",");
work=replaced.replace("\":\"", ":");
replaced=work.replace("\"","")+",definedend";
System.out.println(replaced);
while (!replaced.equals("definedend"))
{
out.add(replaced.substring(0,replaced.indexOf(":"))+","+replaced.substring(replaced.indexOf(":")+1, replaced.indexOf(","))+";");
String tempstring=replaced.substring(replaced.indexOf(",")+1);
replaced=tempstring;
index++;
System.out.println("loop disassembly step"+index+" "+replaced);
}
return out;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.