Json generated by php is transmitted to androidgson and cannot be parsed.

Source: Internet
Author: User
Tags php json
1. the json generated by phpjson_encode is transmitted to android and cannot be parsed by gson. $ arrarray (& #039; token & #039; & amp; gt; & #039; 111 & #039;, & #039; id & #039; & amp; gt; & #039; 1 & #039;, & #039; contacts & #039; & amp; gt; array (& #039; name & #039; & amp; gt; & #039; 11 & #039;, & #039; tel & #039; & amp; gt; & #039; 188 & # 1.php json generated by json_encode is transmitted to android and cannot be parsed by gson.

2. $ arr = array ('Token' => '000000', 'id' => '1', 'contacts' => array ('name' => '11 ', 'tel '=> '123 '));
This array contains arrays converted to json and sent to android. gson cannot parse the array.

PHP multi-dimensional association Array Using json_encode to generate json string android gson does not recognize
If the index array is used

Reply content:

1. json generated by php json_encode is transmitted to android and cannot be parsed by gson.

2. $ arr = array ('Token' => '000000', 'id' => '1', 'contacts' => array ('name' => '11 ', 'tel '=> '123 '));
This array contains arrays converted to json and sent to android. gson cannot parse the array.

PHP multi-dimensional association Array Using json_encode to generate json string android gson does not recognize
If the index array is used

We recommend that you use GsonFormat in Android to integrate it into AS to easily produce a suitable javaBean. If the parsing code on Android is correct, there will be no problem.
The following is the javaBean generated by using this tool corresponding to your json:

public class ceshi {    /**     *token : 111     *id : 1     *contacts : {"name":"11","tel":"188"}     */    private String token;    private String id;    /**     *name : 11     *tel : 188     */    private ContactsBean contacts;    public String getToken() {        return token;    }    public void setToken(String token) {        this.token = token;    }    public String getId() {        return id;    }    public void setId(String id) {        this.id = id;    }    public ContactsBean getContacts() {        return contacts;    }    public void setContacts(ContactsBean contacts) {        this.contacts = contacts;    }    public static class ContactsBean {        private String name;        private String tel;        public String getName() {            return name;        }        public void setName(String name) {            this.name = name;        }        public String getTel() {            return tel;        }        public void setTel(String tel) {            this.tel = tel;        }    }}

Do you want to send the converted json and the entity class you use to receive json?

Different languages may have different implementations for different versions of json-parsed libraries.
Php is built in, so it is easy to call. Some languages need to use third-party class libraries.
Therefore, you can simply ask android what the received string is, and then you are using php json_decode to see if it can be parsed.

Gson cannot parse the json string of php. It mainly depends on whether the json string received by the Android client is complete. If it is complete, it is converted to a java object when using gson, although gson supports generics, you must at least provide the javaBean instance corresponding to the json string. Otherwise, the conversion will fail.

$arr = array('token'=>'111','id'=>'1','contacts'=>array('name'=>'11','tel'=>'188'));

In java, a class must be matched. The class must have String token, int id, and Object contacts, while contacts must have the String name and String tel attributes.

Test. java

package test.joyven.com;public class Test {    private int id;    private String token;    private Contacts contacts;    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getToken() {        return token;    }    public void setToken(String token) {        this.token = token;    }    public Contacts getContacts() {        return contacts;    }    public void setContacts(Contacts contacts) {        this.contacts = contacts;    }    }

Contacts. java

package test.joyven.com;public class Contacts {    private String name;    private String tel;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public String getTel() {        return tel;    }    public void setTel(String tel) {        this.tel = tel;    }    }

Print the json string and check the json structure. I guess your java bean format is incorrect.

Model[] models = new Gson().fromJson(JsonStr, Model[].class);
public class Model{    public int id;    public String token;    public Contacts[] contacts;}public class Contacts {    public String name;    public String tel;}

The simplest way is to use Map Parse your json and perform forced conversion when an object is encountered. Although not elegant, it is common. It is easier for those who are not quite clear about Bean writing. All the above replies are bean-related statements.

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.