在android中解析多个Json对象

在android中解析多个Json对象,android,json,Android,Json,我正在尝试将此json发送到web服务器: [{"codemenu":"1","name":"Fried Rice"}, {"codemenu":"2","name":"Hongkong Fried Rice"}, {"codemenu":"3","name":"Special fried Rice"}] 这是代码,但不起作用: HttpPost httppost = new HttpPost("http://10.0.2.2/pnir_restoran/test.php"); JSONObj

我正在尝试将此json发送到web服务器:

[{"codemenu":"1","name":"Fried Rice"},
{"codemenu":"2","name":"Hongkong Fried Rice"},
{"codemenu":"3","name":"Special fried Rice"}]
这是代码,但不起作用:

HttpPost httppost = new HttpPost("http://10.0.2.2/pnir_restoran/test.php");
JSONObject json = new JSONObject();
try {
    // JSON data:
    json.put("codemenu", "1");
    json.put("name", "friedrice");
    json.put("codemenu", "2");
    json.put("name", "Hongkong friedrice");
    json.put("codemenu", "3");
    json.put("name", "Special friedrice");          

    JSONArray postjson=new JSONArray();
    postjson.put(json); //i cant use postjson.add(json);

    // Post the data:
    httppost.setHeader("json",json.toString());
    httppost.getParams().setParameter("jsonpost",postjson);

    // Execute HTTP Post Request
    System.out.print(json);
    HttpResponse response = httpclient.execute(httppost);
我该怎么办?请帮助我。

您需要使用a,然后将单个s放入阵列中:

// Initialize the JSON Array and your three seperate objects.
JSONArray jsonArray = new JSONArray();
JSONObject jObj1 = new JSONObject();
JSONObject jObj2 = new JSONObject();
JSONObject jObj3 = new JSONObject();

// Put elements in one object at a time and put them in your array.
jObj1.put("codemenu", "1");
jObj1.put("name", "friedrice");

jsonArray.put(jObj1);

jObj2.put("codemenu", "2");
jObj2.put("name", "Hongkong friedrice");

jsonArray.put(jObj2);

jObj3.put("codemenu", "3");
jObj3.put("name", "Special friedrice");

jsonArray.put(jObj3);
您需要使用,然后在阵列中放置单个的:

// Initialize the JSON Array and your three seperate objects.
JSONArray jsonArray = new JSONArray();
JSONObject jObj1 = new JSONObject();
JSONObject jObj2 = new JSONObject();
JSONObject jObj3 = new JSONObject();

// Put elements in one object at a time and put them in your array.
jObj1.put("codemenu", "1");
jObj1.put("name", "friedrice");

jsonArray.put(jObj1);

jObj2.put("codemenu", "2");
jObj2.put("name", "Hongkong friedrice");

jsonArray.put(jObj2);

jObj3.put("codemenu", "3");
jObj3.put("name", "Special friedrice");

jsonArray.put(jObj3);

您可以使用,而不是手动执行所有JSON处理。

您可以使用,而不是手动执行所有JSON处理。

您是否显示了正在生成的JSON?生成的不是您想要的。(您创建一个JSON对象并在其中放入6个条目。您列出的JSON原型包含3个对象,每个对象有2个条目。)(您将JSON对象放入头中,将数组放入参数中也没有意义。)是的,我想在php@hotlickswave中读取的json显示了正在生成的json吗?生成的不是您想要的。(您创建一个JSON对象并在其中放入6个条目。您列出的JSON原型包含3个对象,每个对象有2个条目。)(您将JSON对象放入头中,将数组放入参数中也没有意义。)是的,我想要用php阅读的json@HotLicks@stefan别忘了它是否解决了你的问题。但这是静态的。如果数据为100,我该怎么办?我无法生成json对象的数组,@TusharDhoot@stefan使用一个循环,循环中的代码需要在内部重复。@stefan不要忘记它是否解决了您的问题。但这是静态的。如果数据为100,我应该怎么做?我无法生成json对象数组@TusharDhoot@stefan对需要在内部重复的代码使用循环。