Java Arraylist到字符串并返回到Arraylist
我想将TestObject的ArrayList转换为字符串,反之亦然。我还附加了主要活动,以便您可以看到我要创建的方法 MainActivity:制作ArrayList并需要将其转换为字符串的地方Java Arraylist到字符串并返回到Arraylist,java,android,arraylist,Java,Android,Arraylist,我想将TestObject的ArrayList转换为字符串,反之亦然。我还附加了主要活动,以便您可以看到我要创建的方法 MainActivity:制作ArrayList并需要将其转换为字符串的地方 public class MainActivity extends AppCompatActivity { ArrayList<TestObject> testObjects; @Override protected void onCreate(Bundle savedInstanceS
public class MainActivity extends AppCompatActivity {
ArrayList<TestObject> testObjects;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
testObjects = new ArrayList<>();
testObjects.add(new TestObject("Name1", "Attribute1"));
testObjects.add(new TestObject("Example", "Example"));
}
private String convertObjectArrayToString(ArrayList<TestObject> arrayToBeConverted){
return null;
}
private ArrayList<TestObject> convertStringToObjectArray(){
return null;
}
}
//ArrayList到字符串转换:
String listToJson = new Gson().toJson(testObjects);
//要获取数组列表的ViceVersa字符串:
Type listType = new TypeToken<List>() {}.getType();
List myModelList = new Gson().fromJson(listToJson, listType);
更新:
myModelList = gson.fromJson(br, new TypeToken<ArrayList< TestObject >>(){}.getType());
在onCreate方法中首先填充arraylist:
现在调用将arraylist转换为字符串的方法
private String convertObjectArrayToString(ArrayList<TestObject> arrayToBeConverted){
String listString = "";
for (String s : arrayToBeConverted)
{
listString += s + "\t";
}
System.out.println(listString);
return listString;
}
字符串到arraylist的实现方法
private ArrayList<TestObject> convertStringToObjectArray(){
Gson gson = new Gson();
TypeToken<ArrayList<Publication>> token = new TypeToken<ArrayList<TestObject>>() {
};
ArrayList<TestObject> pb = gson.fromJson(str, token.getType());
return testObjects ;
}
更改您的数据类:TestObject
public class TestObject {
private String name;
private String attribute;
TestObject(String name, String attribute){
this.name = name;
this.attribute = attribute;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAttribute() {
return attribute;
}
public void setAttribute(String attribute) {
this.attribute = attribute;
}
}
用于将中的ArrayList转换为字符串:
ArrayList<TestObject> testObjects;
testObjects = new ArrayList<>();
testObjects.add(new TestObject("Name1", "Attribute1"));
testObjects.add(new TestObject("Example", "Example"));
for (int i=0;i<testObjects.size();i++)
{
String name = testObjects.get(i).getName();
String attribute = testObjects.get(i).getAttribute();
System.out.println(name);
System.out.println(attribute);
}
You will get the items in the array list as Strings....
实现这两种方法的位置?方法位于主要活动ConverObjectArrayString和convertStringToObjectArray中。将getter和setter添加到TestObject中class@CagriYalcin现在将它们添加到代码中,你能分享示例吗?嗨,我喜欢代码,我没有经常使用GSON,但我有一些问题。第一个问题是for循环,它希望我将其更改为对象而不是字符串,您对此有何建议。另一个问题是TypeTokens,应该是TestObject,因为它无法识别变量。您可以将arraylist映射到自定义对象,TypeToken是Gson的类。您好,我尝试过转换回来,但我得到[{attribute=Attribute1,name=Name1},{attribute=Example,name=Example}]作为响应。如何将其创建回TestObject表单?ArrayList到字符串转换为JSON而不是字符串。两者都是不同的东西
public class TestObject {
private String name;
private String attribute;
TestObject(String name, String attribute){
this.name = name;
this.attribute = attribute;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAttribute() {
return attribute;
}
public void setAttribute(String attribute) {
this.attribute = attribute;
}
}
ArrayList<TestObject> testObjects;
testObjects = new ArrayList<>();
testObjects.add(new TestObject("Name1", "Attribute1"));
testObjects.add(new TestObject("Example", "Example"));
for (int i=0;i<testObjects.size();i++)
{
String name = testObjects.get(i).getName();
String attribute = testObjects.get(i).getAttribute();
System.out.println(name);
System.out.println(attribute);
}
You will get the items in the array list as Strings....