How does Android use Gson to convert data to List <Map <string, string>

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.