繁体   English   中英

接收具有2个数组的JSON对象

[英]Recieve JSON Object with 2 Arrays

我在使用JSON时确实是新手,我的Web服务有问题。 使用普通的JSON对象,我没有问题。 我想从Web服务中获取两个数组(字符串和整数),所以我试图将它们放入两个JSON Array,并将这两个放入JSON对象。 现在我希望他们进入我的Android应用程序,但我只是遇到错误。

public static String constructJSON(Integer[] array, String[] array2) {
    JSONObject mainObj = new JSONObject();
    try {
    JSONObject firstArray = new JSONObject();
    firstArray.put("array0", array[0]);
    firstArray.put("array1", array[1]);
    firstArray.put("array2", array[2]);
    firstArray.put("array3", array[3]);
    firstArray.put("array4", array[4]);


    JSONObject secondArray = new JSONObject();
    secondArray.put("sArray0", array2[0]);
    secondArray.put("sArray1", array2[1]);
    secondArray.put("sArray2", array2[2]);
    secondArray.put("sArray3", array2[3]);
    secondArray.put("sArray4", array2[4]);

    JSONArray JArr = new JSONArray();
    JArr.put(firstArray);
    JArr.put(secondArray);

    mainObj.put("arrays", JArr);
    } catch (JSONException e) {
    }
    return mainObj.toString();
} 

现在是Android Studio中的方法:

private void getBW(String krankheit) {
        RequestParams params = new RequestParams();
        params.put("krankheit", krankheit);
        // Invoke RESTful Web Service with Http parameters
        AsyncHttpClient client = new AsyncHttpClient();
        client.get(url, params, new AsyncHttpResponseHandler() {


            @Override
            public void onSuccess(String response) {
                try {
                    // JSON Object
                    JSONObject obj = new JSONObject(response);
                    JSONArray firstJsonArr= obj.getJSONArray("array1");
                    JSONArray secondJsonArr= obj.getJSONArray("array2");
                    for (int k = 0; k < 5; k++) {
                        Bewertung1[k] = (Integer) firstJsonArr.get(k);

                    }
                    for (int j = 0; j < 5; j++) {
                        medikament[j] = (String) secondJsonArr.get(j);

                    }



                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    Toast.makeText(getApplicationContext(), "Error Occured [Server's JSON response might be invalid]!", Toast.LENGTH_LONG).show();
                    e.printStackTrace();

                }


            }
        });

        }

我尝试了不同的解决方案,但是没有一个对我有用。 我希望你们能帮助我。

我建议您使用Android的gson库。 它提供了简单的功能来解析json对象。 看看这个库: https : //github.com/google/gson

您切换了功能,而不是想要的功能[{} {}]

尝试这个:

public static String constructJSON(Integer[] array, String[] array2) {
    try {
    JSONArray firstArray = new JSONArray ();
    firstArray.put( array[0]);
    firstArray.put( array[1]);
    firstArray.put( array[2]);
    firstArray.put( array[3]);
    firstArray.put( array[4]);


    JSONArray secondArray = new JSONArray ();
    secondArray.put(array2[0]);
    secondArray.put( array2[1]);
    secondArray.put( array2[2]);
    secondArray.put( array2[3]);
    secondArray.put( array2[4]);

    JSONObject JArr = new JSONObject();
    JArr.put("firstArr",firstArray);
    JArr.put("secondArr", secondArray);
    return JArr.toString();

    } catch (JSONException e) {
    }
     return null;
} 

这将为您提供所需的JSon,这是使用它的主要代码:

JSONObject obj = new JSONObject(response);
JSONArray firstJsonArr= obj.getJSONArray("firstArr");
JSONArray secondJsonArr= obj.getJSONArray("secondArr");
for (int k = 0; k < firstJsonArr.size(); k++) {
    Log.e("item "+k,"item data : "+firstJsonArr.get(k));
}
for (int j = 0; j < secondJsonArr.size(); j++) {
    Log.e("item "+j,"item data : "+secondJsonArr.get(j));
}

这对实践很有用,但是稍后您应该使用一个库来为您做这类事情,例如Gson等。

暂无
暂无

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

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