2

我正在尝试在 android 中使用带有 JSON 的 PHP Web 服务

我从服务器获得响应数据为

string(170) "["14","Samsung","1","15","Nokia","1","16","Sony Ericson","1","18","LG","1","19","Iphone","1","21","HTC","1","22","Motorola","1","23","Micromax","1","41","BlackBerry","1"]"

我应该如何解析它以在 ListView 上显示它。我熟悉如何通过创建自定义适配器在 ListView 上显示数据,但我无法解析此输出。请帮助

我按照本教程访问 PHP Web 服务

http://www.codeproject.com/Articles/267023/Send-and-receive-json-between-android-and-php
给我一些参考。任何帮助表示赞赏。
谢谢...

Alos 我想知道在 php 端生成的 JSONArray 的正确格式是什么?

4

3 回答 3

4

首先它不是一个有效的Json格式,Json必须有一个label和对应的valueex。"label": "value",但您仍然可以像这样手动解析此字符串

String yourString= "["14","Samsung","1","15","Nokia","1","16","Sony Ericson","1","18","LG","1","19","Iphone","1","21","HTC","1","22","Motorola","1","23","Micromax","1","41","BlackBerry","1"]";

yourString= yourString.subString(1, yourString.length()-1);

String[] arrayValue= yourString.split(",");

现在你有arrayValue[0]= "14" arrayValue[1]= "Samsung"等等。

于 2012-04-23T13:23:30.457 回答
3

看起来像一个“愚蠢的json”数组。基本上没有问题,因为您将其视为字符串列表。通常我会建议使用一些 JSON 类,但在这种特殊的简单情况下,以下就足够了:

String jsonString = "["14","Samsung","1","15","Nokia","1","16","Sony Ericson","1","18","LG","1","19","Iphone","1","21","HTC","1","22","Motorola","1","23","Micromax","1","41","BlackBerry","1"]"
jsonString = jsonString.subString(1, substring.length() -1);
String[] singleValues = jsonString.split(",");

这应该导致这个

new String[]{"14","Samsung","1",....}

并且您可以在自定义 ListAdapter 中传递该字符串数组(但默认的也应该没问题)

于 2012-04-23T13:27:10.303 回答
1

这是有效但奇怪的 json 响应。当你解析这个 json 时,你会得到 27 个值。14, samsung, 1, 15, nokia... 但似乎每 3 项都是像 "14,samsung,1" "15,nokia,1" 这样的对象。而那个json并没有反映这一点。

使用这样的在线 json 解析器,您可以看到可以得到的结果:

http://jsonviewer.stack.hu/

你可以像这样解析那个json:

List<String> list = new ArrayList<String>();
int counter = 0;
boolean first = true;
String json = "[\"14\",\"Samsung\",\"1\",\"15\",\"Nokia\",\"1\",\"16\",\"Sony Ericson\",\"1\",\"18\",\"LG\",\"1\",\"19\",\"Iphone\",\"1\",\"21\",\"HTC\",\"1\",\"22\",\"Motorola\",\"1\",\"23\",\"Micromax\",\"1\",\"41\",\"BlackBerry\",\"1\"]";
JSONArray getJSONArray;
try {
  getJSONArray = new JSONArray(json);

  for (int i = 0; i < getJSONArray.length(); i++) {

    Log.d("", getJSONArray.getString(i) + "-" + i % 1 + "-" + i % 2);

    if ((first == true && counter == 1) || (first == false && counter == 2)) {
      list.add(getJSONArray.getString(i));
      counter = 0;
      first = false;
    }
    else {
      counter += 1;
    }
  }
}
catch (JSONException e) {
  e.printStackTrace();
}

记录您的阵列以查看您得到了什么:

for (String item : list) {
  Log.d("list", item);
}

结果:

D/list    ( 1669): Samsung
D/list    ( 1669): Nokia
D/list    ( 1669): Sony Ericson
D/list    ( 1669): LG
D/list    ( 1669): Iphone
D/list    ( 1669): HTC
D/list    ( 1669): Motorola
D/list    ( 1669): Micromax
D/list    ( 1669): BlackBerry

对于服务器站点:

通常那个json必须是这样的..

[
    {
        id: "14",
        brand: "Samsung",
        status: "1"
    },
    {
        id: "15",
        brand: "Nokia",
        status: "1"
    },
    {
        id: "16",
        brand: "Sony Ericson",
        status: "1"
    }
]
于 2012-04-23T13:33:07.087 回答