gpt4 book ai didi

android - 在android中解析多个Json对象

转载 作者:行者123 更新时间:2023-11-30 04:10:39 25 4
gpt4 key购买 nike

我正在尝试将此 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);

我该怎么办?请帮助我。

最佳答案

您需要使用 JSONArray然后放个人JSONObject 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);

关于android - 在android中解析多个Json对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10907627/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com