如何从 JSON 数组中检索字符串数组?

Posted

技术标签:

【中文标题】如何从 JSON 数组中检索字符串数组?【英文标题】:How tor retrieve String array from JSON Array? 【发布时间】:2020-10-21 17:14:37 【问题描述】:

我有一个 android 应用程序(用 Java 编写),它从后端检索 JSON 对象,对其进行解析,然后在应用程序中显示数据。除一个字段外,一切正常(意味着每个字段都正确显示)。所有正确显示的字段都是String,而导致错误的一个字段是字符串数组!

正在从后端重试的示例对象:


        "attendance_type": "2",
        "guest": [
            "Test Guest",
            "Test Guest 2"
        ],
        "member_id": "1770428",
        "attendance_time": "2020-04-27 04:42:22",
        "name": "HENRY  HHH",
        "last_name": "",
        "email": "henry@mailinator.com",
        "onesignal_playerid": "",
        "user_image": "311591.png",
        "dateOfBirth": "06/22/1997",
        "employeeID": "543210",
        "socialSecurityNumber": "0000"
    

正如我所说,所有字段都被正确检索,除了“guest field”

这是所有内容都被序列化的类:

package com.lu.scanner.ui.attendance.model;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import java.util.List;

public class AttendanceDetails 

    String date;
    @SerializedName("attendance_type")
    private String attendance_type;
    @SerializedName("member_id")
    private String member_id;
    @SerializedName("attendance_date")
    private String attendance_date;
    @SerializedName("name")
    private String name;
    @SerializedName("email")
    private String email;
    @SerializedName("onesignal_playerid")
    private String onesignal_playerid;
    @SerializedName("user_image")
    private String user_image;
    @SerializedName("dateOfBirth")
    private String dateOfBirth;
    @SerializedName("employeeID")
    private String employeeID;
    @SerializedName("socialSecurityNumber")
    private String socialSecurityNumber;
    @SerializedName("attendance_time")
    private String attendance_time;
    @SerializedName("guest")
    private String[] guest;

    public AttendanceDetails(String date) 
        this.date = date;
    

    public String getDate() 
        return date;
    

    public void setDate(String date) 
        this.date = date;
    

    public String getAttendance_type() 
        return attendance_type;
    

    public void setAttendance_type(String attendance_type) 
        this.attendance_type = attendance_type;
    

    public String getMember_id() 
        return member_id;
    

    public void setMember_id(String member_id) 
        this.member_id = member_id;
    

    public String getAttendance_date() 
        return attendance_date;
    

    public void setAttendance_date(String attendance_date) 
        this.attendance_date = attendance_date;
    

    public String getName() 
        return name;
    

    public void setName(String name) 
        this.name = name;
    

    public String getEmail() 
        return email;
    

    public void setEmail(String email) 
        this.email = email;
    

    public String getOnesignal_playerid() 
        return onesignal_playerid;
    

    public void setOnesignal_playerid(String onesignal_playerid) 
        this.onesignal_playerid = onesignal_playerid;
    

    public String getUser_image() 
        return user_image;
    

    public void setUser_image(String user_image) 
        this.user_image = user_image;
    

    public String getDateOfBirth() 
        return dateOfBirth;
    

    public void setDateOfBirth(String dateOfBirth) 
        this.dateOfBirth = dateOfBirth;
    

    public String getEmployeeID() 
        return employeeID;
    

    public void setEmployeeID(String employeeID) 
        this.employeeID = employeeID;
    

    public String getSocialSecurityNumber() 
        return socialSecurityNumber;
    

    public void setSocialSecurityNumber(String socialSecurityNumber) 
        this.socialSecurityNumber = socialSecurityNumber;
    

    public String getAttendance_time() 
        return attendance_time;
    

    public void setAttendance_time(String attendance_time) 
        this.attendance_time = attendance_time;
    

    public String[] getGuest()
        return guest;
    

    public void setGuest(String[] guest)
        this.guest=guest;
    

这是 SQLLite 数据库:

 private static final String CREATE_TABLE_ATTENDANCE_DETAILS = "CREATE TABLE IF NOT EXISTS " + TABLE_ATTENDANCE_DETAILS +
        "( date TEXT , " +
        "attendance_type TEXT, " +
        "member_id TEXT, " +
        "attendance_date TEXT, " +
        "name TEXT, " +
        "email TEXT, " +
        "onesignal_playerid TEXT, " +
        "user_image TEXT, " +
        "dateOfBirth TEXT, " +
        "employeeID TEXT, " +
        "socialSecurityNumber TEXT, " +
        "attendance_time TEXT, " +
        "guest TEXT); ";
       

最后,是检索数据的地方:

public List<AttendanceDetails> getAllAttendanceDetails() 
    List<AttendanceDetails> attendanceDetailsList = new ArrayList<AttendanceDetails>();

    String selectQuery = "SELECT * FROM " + TABLE_ATTENDANCE_DETAILS;
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) 
        do 
            AttendanceDetails attendanceDetails = new AttendanceDetails();
            attendanceDetails.setDate(cursor.getString(0));
            attendanceDetails.setAttendance_type(cursor.getString(1));
            attendanceDetails.setMember_id(cursor.getString(2));
            attendanceDetails.setAttendance_date(cursor.getString(3));
            attendanceDetails.setName(cursor.getString(4));
            attendanceDetails.setEmail(cursor.getString(5));
            attendanceDetails.setOnesignal_playerid(cursor.getString(6));
            attendanceDetails.setUser_image(cursor.getString(7));
            attendanceDetails.setDateOfBirth(cursor.getString(8));
            attendanceDetails.setEmployeeID(cursor.getString(9));
            attendanceDetails.setSocialSecurityNumber(cursor.getString(10));
            attendanceDetails.setAttendance_time(cursor.getString(11));
            attendanceDetails.setGuest(cursor.getString(12));
            
            attendanceDetailsList.add(attendanceDetails);
         while (cursor.moveToNext());
    

    return attendanceDetailsList;

因此,我认为主要的问题是表创建中的TEXT类型与String数组不兼容。另外我认为cursor.String() 函数不适用于"guest" 字符串数组。我该怎么做才能使所有这些代码与"guest" 字段兼容?

注意:除了来宾字段之外,一切都运行良好...

【问题讨论】:

尝试在来宾字段中用 List 替换 String[] 【参考方案1】:

数据库存储数据行,分为列。每列都是一个标量。 SQLite 仅支持(基本上)列的数字和文本。文本列表(或字符串中的数组)不适合其中。您正在尝试将单个字符串分配给数组。

你有两个选择:

    将 guest 建模为自己的表,并使用外键和适当的 JOIN 来获取数据。

    自己编码数据。如果您不想查询数组,但总是检索整个内容,这是更简单的方法:

    Gson gson;
    String guestSerialized = gson.toJson(attendanceDetails.getGuest);
    // Insert data like this
    
    // Retrieve:
    attendanceDetails.setGuest(gson.fromJson(cursor.getString(12), String[].class))
    

也就是说,如果您使用 GSON 进行 JSON(反)序列化。您可以选择不同的格式或库。

【讨论】:

【参考方案2】:

也许您可以在“客人”字段中给出一些名称,例如:

"guest": [
   
    "guest" : "Test Guest",
   ,
   
    "guest" : "Test Guest 2"
    ,
],

然后,为了读取这些值,您可以这样做:

JSONArray ja = (JSONArray) jo.get("guest"); 
Map address = ((Map)jo.get("guest"))

Iterator itr = ja.iterator();
Iterator<Map.Entry> itr1;
          
while (itr.hasNext()) 
   itr1 = ((Map) itr.next()).entrySet().iterator(); 
   while (itr1.hasNext())  
        Map.Entry pair = itr1.next();
        System.out.println(pair.getKey() + " : " + pair.getValue()); 
    
 

其中:pair.getKey 是键的名称(“guest”)pair.getValue 是键的值(“测试客人”)

源代码是here。

对不起,如果我犯了任何错误。请要求澄清! :)

【讨论】:

以上是关于如何从 JSON 数组中检索字符串数组?的主要内容,如果未能解决你的问题,请参考以下文章

从 SQLite 中检索 JSON 数据时,反斜杠显示在数组中

如何通过匹配(字符串到数组和数组到数组)从mysql存储和检索数组数据我在从mysql获取数据时遇到问题

解析 JSON 数据时无法将字符串附加到数组

PHP,Laravel:从 JSON 中消除特定的复杂数组

如何从 JSON 数组中获取字符串数组? [关闭]

如何从字典中检索数组