Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/181.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何将字符串转换为Json和it信息中的extrat_Java_Android_Json - Fatal编程技术网

Java 如何将字符串转换为Json和it信息中的extrat

Java 如何将字符串转换为Json和it信息中的extrat,java,android,json,Java,Android,Json,我使用Reformation2和Rxjava2从mongodb和nodeJs服务器插入/获取信息,现在,我接收所有数据作为字符串,但我想从我的基础中获取孔集合信息,所以我需要将字符串转换为JSON并获取每个信息 接收数据的我的代码: 1-服务: @POST("collect/get") @FormUrlEncoded Observable<String> getcollection(@Field("selector") String selector); 3-接收

我使用Reformation2和Rxjava2从mongodb和nodeJs服务器插入/获取信息,现在,我接收所有数据作为字符串,但我想从我的基础中获取孔集合信息,所以我需要将字符串转换为JSON并获取每个信息

接收数据的我的代码:

1-服务:

 @POST("collect/get")
    @FormUrlEncoded
    Observable<String> getcollection(@Field("selector") String selector);
3-接收功能

private void getallcollection(String selector) {

        compositeDisposable.add(myServices.getcollection(selector)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<String>(){
                    @Override
                    public void accept(String s) throws Exception {
                        Log.d("infos",s);
                    }
                }));
    }
实际上,我收到了所有数据,控制台显示:[{“\u id”:“5e22074673c926147c3a73f5”,“创建日期”列:“17-01-2020”,“创建者”:“Alaeddine”,“emailcol”:amir@gmail.com,“姓名”:“埃米尔”,“隆格”:“10.17932686969547367”,“拉特科尔”:“36.83353893150942”,“电话费克斯”:“123”,“电话费克斯”:“1234”,“地址科尔”:“巴黎路34号”,“休尔马丁科尔”:“7”、“heure_apresmatin_col”:“5”、“type”:“collection”、“imagePath”:“mmmmmmmm”}]


我想知道如何从这个Json中提取例如creator。

您可以使用第三方Json解析器,就像您已经为Android开发的那样。Java似乎不包含内置Json解析器


.

当我像你说的那样使用谷歌GSON时:`GSON GSON=new GSON();col c=GSON.fromJson(s,col.class);Log.d(“creator”,c.getCreator())`我收到错误io.reactivex.exceptions.OneErrorNotImplementedException:java.lang.IllegalStateException:应为BEGIN_对象,但在第1行第2列路径$处为BEGIN_数组。GSON似乎不接受数组作为最外层的JSON元素。您可以将数组包装成如下对象:
String obj=“{\'ARRAY\:“+s+”";可能有输入错误,但你明白了…我用gson来解决它,比如
List heroList=response.body();List colList=response.body();for(col c:colList){Log.d(“name:,c.getNom_col());}
private void getallcollection(String selector) {

        compositeDisposable.add(myServices.getcollection(selector)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<String>(){
                    @Override
                    public void accept(String s) throws Exception {
                        Log.d("infos",s);
                    }
                }));
    }
public class col {
    private String creator;
    private String emailcol;
    private String date_creation_col;
    private String nom_col;
    private String long_col;
    private String lat_col;
    private String tel_fix_col;
    private String tel_mobile_col;
    private String creatorcreator;
    private String heure_matin_col;
    private String heure_apresmatin_col;
    private String type;
    private String imagePath;

    public col(String creator, String emailcol, String date_creation_col, String nom_col, String long_col, String lat_col, String tel_fix_col, String tel_mobile_col, String creatorcreator, String heure_matin_col, String heure_apresmatin_col, String type, String imagePath) {
        this.creator = creator;
        this.emailcol = emailcol;
        this.date_creation_col = date_creation_col;
        this.nom_col = nom_col;
        this.long_col = long_col;
        this.lat_col = lat_col;
        this.tel_fix_col = tel_fix_col;
        this.tel_mobile_col = tel_mobile_col;
        this.creatorcreator = creatorcreator;
        this.heure_matin_col = heure_matin_col;
        this.heure_apresmatin_col = heure_apresmatin_col;
        this.type = type;
        this.imagePath = imagePath;
    }

    public String getCreator() {
        return creator;
    }

    public void setCreator(String creator) {
        this.creator = creator;
    }

    public String getEmailcol() {
        return emailcol;
    }

    public void setEmailcol(String emailcol) {
        this.emailcol = emailcol;
    }

    public String getDate_creation_col() {
        return date_creation_col;
    }

    public void setDate_creation_col(String date_creation_col) {
        this.date_creation_col = date_creation_col;
    }

    public String getNom_col() {
        return nom_col;
    }

    public void setNom_col(String nom_col) {
        this.nom_col = nom_col;
    }

    public String getLong_col() {
        return long_col;
    }

    public void setLong_col(String long_col) {
        this.long_col = long_col;
    }

    public String getLat_col() {
        return lat_col;
    }

    public void setLat_col(String lat_col) {
        this.lat_col = lat_col;
    }

    public String getTel_fix_col() {
        return tel_fix_col;
    }

    public void setTel_fix_col(String tel_fix_col) {
        this.tel_fix_col = tel_fix_col;
    }

    public String getTel_mobile_col() {
        return tel_mobile_col;
    }

    public void setTel_mobile_col(String tel_mobile_col) {
        this.tel_mobile_col = tel_mobile_col;
    }

    public String getCreatorcreator() {
        return creatorcreator;
    }

    public void setCreatorcreator(String creatorcreator) {
        this.creatorcreator = creatorcreator;
    }

    public String getHeure_matin_col() {
        return heure_matin_col;
    }

    public void setHeure_matin_col(String heure_matin_col) {
        this.heure_matin_col = heure_matin_col;
    }

    public String getHeure_apresmatin_col() {
        return heure_apresmatin_col;
    }

    public void setHeure_apresmatin_col(String heure_apresmatin_col) {
        this.heure_apresmatin_col = heure_apresmatin_col;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getImagePath() {
        return imagePath;
    }

    public void setImagePath(String imagePath) {
        this.imagePath = imagePath;
    }
}