I now have json data of any length, which is generated using json_encode () of php, in the following format {code ...} now we want to convert it to the format of List & amp; lt; Map & amp; lt; String, String & amp; gt; and display it with listView. The first key of hashmap corresponds to "Name", and the second key... I now have json data of any length, which is generated using json_encode () of php, in the following format:
[{"rowID":"1","Name":"tqtqwet","Comment":"qewrtqwe"},{"rowID":"2","Name":"waf","Comment":"agsadga"},{"rowID":"3","Name":"android","Comment":"fafdadsf"},{"rowID":"4","Name":"android222","Comment":"fasdfas"},{"rowID":"5","Name":"fadfa","Comment":"adgasd"}]
Now I want to convert it to List > Format, and display it with listView. The first key of hashmap corresponds to "Name", the second key corresponds to "Comment", and the rowID is ignored. How can this conversion be completed as quickly as possible? If there is a way to bypass it, you can also! Thank you.
Reply content:
I now have json data of any length, which is generated using json_encode () of php, in the following format:
[{"rowID":"1","Name":"tqtqwet","Comment":"qewrtqwe"},{"rowID":"2","Name":"waf","Comment":"agsadga"},{"rowID":"3","Name":"android","Comment":"fafdadsf"},{"rowID":"4","Name":"android222","Comment":"fasdfas"},{"rowID":"5","Name":"fadfa","Comment":"adgasd"}]
Now I want to convert it to List > Format, and display it with listView. The first key of hashmap corresponds to "Name", the second key corresponds to "Comment", and the rowID is ignored. How can this conversion be completed as quickly as possible? If there is a way to bypass it, you can also! Thank you.
The json data format is very simple and can be parsed without the complexity you think;
You need a JavaBean object:
CustomClass. java:
public class CustomClass { private String Name; private String Comment; public String getName() { return Name; } public void setName(String name) { Name = name; } public String getComment() { return Comment; } public void setComment(String comment) { Comment = comment; } }
MainActivity. java:
Public class MainActivity extends Activity {// your json data String jsonString = "[{\" rowID \ ": \" 1 \ ", \" Name \": \ "tqtqwet \", \ "Comment \": \ "qewrtqwe \" },{ \ "rowID \": \ "2 \", \ "Name \": \ "waf \", \ "Comment \": \ "agsadga \" },{ \ "rowID \": \ "3 \", \ "Name \": \ "android \", \ "Comment \": \ "fafdadsf \" },{ \ "rowID \": \ "4 \", \ "Name \": \ "android222 \", \ "Comment \": \ "fasdfas \" },{ \ "rowID \": \ "5 \", \ "Name \": \ "fadfa \", \ "Comment \": \ "adgasd \"}] "; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); Type listType = new TypeToken> (){}. getType (); ArrayList
CustomList = new Gson (). fromJson (jsonString, listType );}}
CustomList is the set with json data you want.customList.get(index).getName() or getXXX()
Get the value you want