获取没有字符串名称的jsonarray [英] Get jsonarray without string name

查看:109
本文介绍了获取没有字符串名称的jsonarray的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取不带字符串名称的json数组:

i need get json array without string name:

代码选择Sqlite:

Code Select Sqlite:

public Cursor getAllData() {
    String selectQuery = "Select * from capturas";
    SQLiteDatabase db = new MyHelper(this).getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    return cursor;
}

Code Json:

Code Json :

public JSONObject createJsonObject() throws JSONException {
    Cursor cursor = getAllData();
    JSONObject jobj;
    JSONArray arr = new JSONArray();
    cursor.moveToFirst();
    while (cursor.moveToNext()) {
        jobj = new JSONObject();
        jobj.put("sCaptID", cursor.getString(0));
        jobj.put("sName", cursor.getString(1));
        arr.put(jobj);
    }
    jobj = new JSONObject();


    jobj.put("data", arr);
    return jobj;
}

我需要不带数据的jobj.put。

I need the jobj.put without "data".

代码JsonSend

Code JsonSend

  public void postJsonToServer() throws JSONException {
    JSONObject js = createJsonObject();
    String url = "http://xx.1xx.xx9.xx/server";
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(com.android.volley.Request.Method.POST, url, js, new com.android.volley.Response.Listener<JSONObject>() {

结果json是:

 {"data":[{
    {
     "id": "22",
     "name": "test"
}]
}

我需要json结果:

    [
    {
     "id": "22",
     "name": "test"
}]


推荐答案

尝试使用空的字符串键

因此替换

jobj.put("data", arr);

jobj.put("", arr);




UPDATE

UPDATE

结果是:{[ {{ id: 22, name: test}]} ....---我需要.....--- [{ id: 22, name :测试} ]

Result is : {[{ { "id": "22", "name": "test" }] } ....--- i need .....---[ { "id": "22", "name": "test" }]

现在尝试使用 Google GSON库格式化所需的JSONObject,

Now try to use Google GSON library to format the JSONObject you want,

将以下依赖项添加到build.gradle模块级别。

add below dependency into build.gradle module level.

implementation 'com.google.code.gson:gson:2.8.5'

并将 createJsonObject()更改为:

public JSONObject createJsonObject() throws JSONException {
    Cursor cursor = getAllData();
    JSONObject jobj;
    JSONArray arr = new JSONArray();
    cursor.moveToFirst();
    while (cursor.moveToNext()) {
        jobj = new JSONObject();
        jobj.put("sCaptID", cursor.getString(0));
        jobj.put("sName", cursor.getString(1));
        arr.put(jobj);
    }

    Gson gson = new Gson();
    String json = gson.toJson(arr, new TypeToken<JSONArray>() {
    }.getType());

    json = "{" + json + "}";
    jobj = new JSONObject(json);

    return jobj;
}

这篇关于获取没有字符串名称的jsonarray的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆