繁体   English   中英

javascript 字符串转换为对象/数组

[英]javascript string into object/array

我从 java 代码接收该字符串: {devices:[3962], contacts:[{_userId='1', _contactId='(+1)1111111', _contactName='Name Surname'}]}我想将其转换为正常的 object,但它不是有效的 JSON 格式。 你有什么想法?

或者也许有人可以用 JAVA 代码帮助我。 我想所有的魔法都在这里:

    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }
    public String extractDbDump() {
        return "{devices:" + getDevices() + ", contacts:" + getContacts() + "}";
    }
    private String getContacts() {
        List<ContactItem> contactList = new ArrayList<>();
        String selectQuery = "SELECT * FROM " + TABLE_CONTACT;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        if (cursor.moveToFirst()) {
            do {
                ContactItem contact = new ContactItem();
                contact.userId = cursor.getString(0);
                contact.contactName = cursor.getString(1);
                contact.contactId = cursor.getString(2);
                contactList.add(contact);
            } while (cursor.moveToNext());
        }
        db.close();
        return "" + contactList.toString();
    }
    private String getDevices() {
        List<String> deviceList = new ArrayList<>();
        String selectQuery = "SELECT * FROM " + TABLE_DEVICE;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        if (cursor != null && cursor.getCount() > 0) {
            if (cursor.moveToFirst()) {
                do {
                    deviceList.add(cursor.getString(0));
                } while (cursor.moveToNext());
            }
        }
        db.close();
        return deviceList.toString();
    }
    public class ContactItem {
        public String userId;
        public String contactId;
        public String contactName;
        @Override
        public String toString() {
            return "{" +
                    "_userId='" + userId + '\'' +
                    ", _contactId='" + contactId + '\'' +
                    ", _contactName='" + contactName + '\'' +
                    '}';
        }
    }


我更改了您的 java 并改为传递 JSONObject 和 JSONArray。 它将为您的 javascript 程序返回 json object。

public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    
    public JSONObject extractDbDump() {
        JSONObject json = new JSONObject();
        json.put("devices", getDevices());
        json.put("contacts", getContacts());
        return json;
    }

    private JSONArray getContacts() {
        JSONArray array = new JSONArray();
        String selectQuery = "SELECT * FROM " + TABLE_CONTACT;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        if (cursor.moveToFirst()) {
            do {
                JSONObject item = new JSONObject();
                item.put("_userId", cursor.getString(0));
                item.put("_contactId", cursor.getString(1));
                item.put("_contactName",cursor.getString(2));
                array.put(item);

            } while (cursor.moveToNext());
        }
        db.close();
        return array;
    }

    private JSONArray getDevices() {
        JSONArray array = new JSONArray();
        String selectQuery = "SELECT * FROM " + TABLE_DEVICE;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);
        if (cursor != null && cursor.getCount() > 0) {
            if (cursor.moveToFirst()) {
                do {
                    array.put(cursor.getString(0));
                } while (cursor.moveToNext());
            }
        }
        db.close();
        return array;
    }
}


处理此案例的最佳方法是要求Java开发人员更改格式以使用有效的 JSON 代码进行响应。 然后你可以只调用JSON.parse就可以了。

另一种方法是编写某种代码,使其有效 JSON。 为此,您需要将每个键包装成"引号并将=替换为:符号。

您可以尝试快速修复字符串。 像这样的东西:

 var s = `{numbers:[3962], contacts:[{_userId='1', _contactId='(+1)1111111', _contactName='Name Surname'}]}`; var obj = eval(s.replace(/=/g, ":").replace(/:\[/g, "=[")); // <-- bad practice.;! console.log(obj);

但这是个坏主意。 我发布它只是作为临时紧急情况的最后手段。 不要在严肃的代码中使用它。

您需要修复 Java 代码。

您需要更改以下内容。

  1. ContactItem.toString

     public String toString() { return "{" + "\"_userId\":\"" + userId + '"' + ", \"_contactId\":\"" + contactId + '"' + ", \"_contactName\":\"" + contactName + '"' + '}'; }
  2. extractDbDump

public String extractDbDump() {
    return "{\"devices\":" + getDevices() + ", \"contacts\":" + getContacts() + "}";
}

现在结果应该是{"devices":[3962], "contacts":[{"_userId":"1", "_contactId":"(+1)1111111", "_contactName": "Name Surname"}]}

现在您可以运行JSON.parse

 const response = `{"devices":[3962], "contacts":[{"_userId":"1", "_contactId":"(+1)1111111", "_contactName": "Name Surname"}]}`; console.log(JSON.parse(response));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM