Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.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 创建JSON ArrayList/JSON对象并发送到Android中的PHP_Java_Php_Android_Json_Arraylist - Fatal编程技术网

Java 创建JSON ArrayList/JSON对象并发送到Android中的PHP

Java 创建JSON ArrayList/JSON对象并发送到Android中的PHP,java,php,android,json,arraylist,Java,Php,Android,Json,Arraylist,我在JAVA中有两个字符串ArrayList(问题,答案)。我必须将这两个数组列表放在一个JSONobject或JSONArraylist中,并需要作为json对象或ArrayList发送到php。在那里,我必须逐个迭代两个数组列表 我想得到json对象或类似arraylist的 $array1=array(2, 4);\\First Requested array $array2=array(5, 96);\\Second Requested array $array = array(

我在JAVA中有两个字符串ArrayList(问题,答案)。我必须将这两个数组列表放在一个JSONobject或JSONArraylist中,并需要作为json对象或ArrayList发送到php。在那里,我必须逐个迭代两个数组列表

我想得到json对象或类似arraylist的

$array1=array(2, 4);\\First Requested array
$array2=array(5, 96);\\Second Requested array
$array = array(
    'r1' =>$array1 ,
    'r2' => $array2,

    ); 
我不知道如何在arraylist或object中放置两个带的数组

我的ArrayList是

ArrayList<String> Questions = new ArrayList<String>();
ArrayList<String> Answers = new ArrayList<String>();
ArrayList Questions=new ArrayList();
ArrayList Answers=新的ArrayList();
我不知道如何将两个数组放在一个arraylist中,以及如何发送、接收json对象

提前感谢…

请尝试以下代码:

$array1=array(2, 4);\\First Requested array
$array2=array(5, 96);\\Second Requested array
$array['arrayList'] = array('r1' =>$array1 ,'r2' => $array2); 

$jsonString = json_encode($array);
echo $jsonString;
请尝试以下代码:

$array1=array(2, 4);\\First Requested array
$array2=array(5, 96);\\Second Requested array
$array['arrayList'] = array('r1' =>$array1 ,'r2' => $array2); 

$jsonString = json_encode($array);
echo $jsonString;
使用
json\u encode()
如下。
json\u encode()
用于将数组转换为json字符串

$array = array(
    'r1' =>$array1 ,
    'r2' => $array2,

    );
$json = json_encode($array);
echo $json;
有关更多信息,请参见此处使用
json\u encode()
,如下所示。
json\u encode()
用于将数组转换为json字符串

$array = array(
    'r1' =>$array1 ,
    'r2' => $array2,

    );
$json = json_encode($array);
echo $json;
有关更多信息,请参见此处

    ArrayList<String> Questions = new ArrayList<String>();
    ArrayList<String> Answers = new ArrayList<String>();

    JSONArray finalJson = new JSONArray();
    for (int i = 0; i < Questions.size(); i++)
    {
        JSONObject json = new JSONObject();

            json.put("Question", Questions.get(i);

            finalJson.put(json);

    }

    JSONArray finalJson1 = new JSONArray();
    for (int j = 0; j < Answers.size(); i++)
    {
        JSONObject json = new JSONObject();

            json.put("Answer", Answers.get(j);

            finalJson1.put(json);

    }

JSONObject resultjson = new JSONObject();
resultjson.put("Questions", finalJson);
resultjson.put("Answers", finalJson1);
试试这个

    ArrayList<String> Questions = new ArrayList<String>();
    ArrayList<String> Answers = new ArrayList<String>();

    JSONArray finalJson = new JSONArray();
    for (int i = 0; i < Questions.size(); i++)
    {
        JSONObject json = new JSONObject();

            json.put("Question", Questions.get(i);

            finalJson.put(json);

    }

    JSONArray finalJson1 = new JSONArray();
    for (int j = 0; j < Answers.size(); i++)
    {
        JSONObject json = new JSONObject();

            json.put("Answer", Answers.get(j);

            finalJson1.put(json);

    }

JSONObject resultjson = new JSONObject();
resultjson.put("Questions", finalJson);
resultjson.put("Answers", finalJson1);
试试这个:

ArrayList<String> Questions = new ArrayList<String>();
ArrayList<String> Answers = new ArrayList<String>();
ArrayList<String> Q&A = new ArrayList<String>();
JSONObject send = new JSONObject;

    JSONObject jo;
    for (int i = 0; i < your_arrayList.size(); i++) {
       jo = new JSONObject(); // when to loop start it will prom a new JSONObject
       jo.put("STRING_NAME", "FIRST_ARRAYLIST".get[i]); // get your arrayList index
       jo.put("STRING_NAME", "SECOND_ARRAYLIST".get[i]); // get your arrayList index

       Q&A.add(jo.toString()); // pass the JSONObject into your arrayList
    }
    send.put("STRING_NAME", Q&A); //pass your arrayList in your JSONObject
ArrayList Questions=new ArrayList();
ArrayList Answers=新的ArrayList();
ArrayList Q&A=新的ArrayList();
JSONObject send=新的JSONObject;
JSONObject jo;
对于(int i=0;i
然后用你的方法发送:)希望这会有帮助如果你的方法中有arrayList你可以发送Q&A,如果你需要jsonObject你可以发送send jsonObject试试这个:

ArrayList<String> Questions = new ArrayList<String>();
ArrayList<String> Answers = new ArrayList<String>();
ArrayList<String> Q&A = new ArrayList<String>();
JSONObject send = new JSONObject;

    JSONObject jo;
    for (int i = 0; i < your_arrayList.size(); i++) {
       jo = new JSONObject(); // when to loop start it will prom a new JSONObject
       jo.put("STRING_NAME", "FIRST_ARRAYLIST".get[i]); // get your arrayList index
       jo.put("STRING_NAME", "SECOND_ARRAYLIST".get[i]); // get your arrayList index

       Q&A.add(jo.toString()); // pass the JSONObject into your arrayList
    }
    send.put("STRING_NAME", Q&A); //pass your arrayList in your JSONObject
ArrayList Questions=new ArrayList();
ArrayList Answers=新的ArrayList();
ArrayList Q&A=新的ArrayList();
JSONObject send=新的JSONObject;
JSONObject jo;
对于(int i=0;i

然后使用您的方法发送:)希望这将有助于如果您的方法中有arrayList,您可以发送Q&A,如果您需要jsonObject,您可以发送send jsonObject

我在java中创建了两个arrayList。我需要将其转换为json objecti在java中创建了两个arraylist。我需要转换它,因为json objecti遵循了您的代码。但是我不知道我将以何种格式在php中获取json数据。请帮助我…兄弟,我只想要像$array1=array(2,4)这样的数组$array2=数组(5,96);在php.First数组(Question1,question2,…)中,第二个数组(Answer1,Answer2,…)中,我遵循了您的代码。但我不知道在php中获取json数据的格式。请帮助我…兄弟,我只想要像$array1=Array(2,4)这样的数组$array2=数组(5,96);在php.First数组(Question1,question2,…)第二个数组(Answer1,Answer2,…)中,您需要的确切输出是什么?您需要的确切输出是什么?