Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.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 如何在android studio中获取JSON对象的字段?_Java_Android_Arrays_Json_Android Edittext - Fatal编程技术网

Java 如何在android studio中获取JSON对象的字段?

Java 如何在android studio中获取JSON对象的字段?,java,android,arrays,json,android-edittext,Java,Android,Arrays,Json,Android Edittext,我有一个android应用程序和一个Web服务。当我想要编辑一些寄存器时,我必须从数据库中获取整个记录,并将每个字段放入编辑文本中 Web服务将此返回给android应用程序 [{"id":"6","0":"6","tipo":"No Oxidado","1":"No Oxidado","fecha":"2015-02-02","2":"2015-02-02","ubicacion":"-1.555505, -6.6171","3":"-1.555505, -6.6171","persona":

我有一个android应用程序和一个Web服务。当我想要编辑一些寄存器时,我必须从数据库中获取整个记录,并将每个字段放入编辑文本中

Web服务将此返回给android应用程序

[{"id":"6","0":"6","tipo":"No Oxidado","1":"No Oxidado","fecha":"2015-02-02","2":"2015-02-02","ubicacion":"-1.555505, -6.6171","3":"-1.555505, -6.6171","persona":"Laura Morales","4":"Laura Morales","fotografia":"-","5":"-"}]
我有一个名为“result”的变量,它有这个JSON字符串

在我的Android应用程序中,如何将每个字段放在独立的编辑文本中

比如:

txtid.setText(result[0]);
txtType.setText(result[1]);
txtDate.setText(result[2]);
txtLocation.setText(result[3]);
txtPerson.setText(result[4]);

其中“result”是我的JSON字符串。

有一个库可以将JSON对象转换为调用
GSON

首先,使用输入对象创建一个模型,如下所示:

class InputModel {
  String id;
  String 0; //it's better to change this object name
  String tipo;
...
}
接下来,您可以使用
Gson
将输入绑定到模型。应该是这样的:

InputModel mInput = new Gson().fromJson(data);
@SerializedName("id")
String productId;
ps1:数据是您的输入字符串

ps2:如果希望输入的名称与输入的名称不同,可以使用如下注释:

InputModel mInput = new Gson().fromJson(data);
@SerializedName("id")
String productId;
试试下面的代码

如果数组中有多个json对象

String response = "[{"id":"6","0":"6","tipo":"No Oxidado","1":"No Oxidado","fecha":"2015-02-02","2":"2015-02-02","ubicacion":"-1.555505, -6.6171","3":"-1.555505, -6.6171","persona":"Laura Morales","4":"Laura Morales","fotografia":"-","5":"-"}]"

try {
       JSONArray jsonArray = new JSONArray(response);
       for (int i =0; i<jsonArray.length();i++){
           JSONObject jsonObject = jsonArray.getJSONObject(i);
           txtid.setText(jsonObject.getString("0"));
           txtType.setText(jsonObject.getString("1"));
           txtDate.setText(jsonObject.getString("2"));
        }
     } catch (JSONException e) {
         e.printStackTrace();
     }