如何从android中的JSON收集数据没有键值对?

时间:2014-03-12 09:25:27

标签: php android json

我试图在Android中以JSON的形式从服务器收集数据。但是有一些技术故障,因为我必须解析不是key:value对形式的JSON数据。 要么 如何解析在JSON中创建的用户创建的关联数组? 需要帮忙? 提前感谢你。

修改


我想在下面两种情况中解析我收到的JSON对象。

案例1:

PHP脚本如下:

<?php
session_start();

$arraygive = $_SESSION['arraygive'];

$con=mysql_connect("localhost","root","");
mysql_select_db("rrugd");

$output = array();
$output1 = array();

foreach ($arraygive as $lid)
{
    echo "<br>";echo "new pass";echo "<br>";
    $query = "SELECT * FROM places WHERE(LID = '$lid');";
    $result = mysql_query($query) or die(mysql_error());
    $output = mysql_fetch_row($result);
    array_push($output1, $output);
}

print(json_encode($output1));

?>

它创建了以下格式的JSON对象:

[["1","shopknock","0","0","22","18.5123","73.8563"],["2","Food Shopei","231","1","17","18.5122","73.8562"],["10","Ccd","0","0","22","18.5211","73.857"]]

我根本不知道如何在Android中解析这种格式。

案例2:

PHP脚本如下:

<?php
session_start();

$arraygive = $_SESSION['arraygive'];

$con=mysql_connect("localhost","root","");
mysql_select_db("rrugd");

$output = array();
$output1 = array();

foreach ($arraygive as $lid)
{
    echo "<br>";echo "new pass";echo "<br>";
    $query = "SELECT * FROM places WHERE(LID = '$lid');";
    $result = mysql_query($query) or die(mysql_error());
    $output = mysql_fetch_assoc($result);
    array_push($output1, $output);
}

print(json_encode($output1));

?>

它创建了以下格式的JSON对象:

[{"lid":"1","name":"shopknock","rid":"0","cid":"0","ccnt":"22","locx":"18.5123","locy":"73.8563"},{"lid":"2","name":"Food Shopei","rid":"231","cid":"1","ccnt":"17","locx":"18.5122","locy":"73.8562"},{"lid":"10","name":"Ccd","rid":"0","cid":"0","ccnt":"22","locx":"18.5211","locy":"73.857"}]

请注意,Case1和Case 2 PHP脚本之间的唯一区别是 mysql_fetch_row mysql_fetch_assoc


我无法使用我使用的代码解析此JSON对象(在下面的案例3中给出),尽管它适用于案例3.

案例3: 但是,每当从上面的另一个PHP脚本创建上述格式的JSON对象(案例2)时:

<?php
//used for populating list of catagories at different instances
$con=mysql_connect("localhost","root","");
mysql_select_db("rrugd");

$query = "SELECT * FROM places ORDER BY ccnt DESC;";
$result=mysql_query($query) or die(mysql_error());

$output=array();
while($row=mysql_fetch_assoc($result))
{
    $output[]=$row; 
}


print(json_encode($output));

?>

JSON对象有效(从上面的脚本创建,但与案例2中给出的相同)

我用来解析案例3中的JSON对象的Android代码如下:

JSONArray jsonArray = new JSONArray(result);
int length = jsonArray.length();
for (int i = 0; i < length; i++)
 {
            JSONObject jObj = jsonArray.getJSONObject(i);
    String name = jObj.getString(TAG_NAME);
    String rid = jObj.getString(TAG_RID);

    HashMap<String, String> map = new HashMap<String, String>();
    map.put(TAG_RID, rid);
    map.put(TAG_NAME, name);

    oslist.add(map);

     ListAdapter adapter = new SimpleAdapter(User_Home_List_Activity.this, oslist,
    R.layout.list_v, new String[] { TAG_NAME }, new int[] { R.id.name});
    l1.setAdapter(adapter);

}

修改


我尝试使用Toasts进行调试,我意识到在上面的代码中(在Try块中)控件没有到达第一行本身。 即。

JSONArray jsonArray = new JSONArray(result);

如果我在上面的行之前在Try块中应用Toast,则会显示它(即控制到达该行)。但是上面一行之后的Toast没有显示出来。

2 个答案:

答案 0 :(得分:1)

尝试这样

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

    JSONObject mJsonObject = new JSONObject();
    mJsonObject = mJsonArray.getJSONObject(i);

    for (int j = 0; j < mJsonObject .length(); j++) {

     int Id = mJsonObject.getString("lid");
     String Name = mJsonObject.getString("name");
     .
     .
     .
   }
}

答案 1 :(得分:0)

我已经尝试过你的Json回应,以及Wola的工作。这是您可以用来解析JSON输出为Case 2

的Json字符串的代码片段
    StringBuilder output = new StringBuilder();
    JSONArray jArr;
    try
    {
        jArr = new JSONArray(jString);

        for (int i = 0; i < jArr.length(); i++)
        {
            JSONObject jObj = jArr.getJSONObject(i);
            output.append("\n\n");
            output.append("\n lid : " + jObj.getInt("lid"));
            output.append("\n name : " + jObj.getString("name"));
            output.append("\n rid : " + jObj.getInt("rid"));
            output.append("\n cid : " + jObj.getInt("cid"));
            output.append("\n ccnt : " + jObj.getInt("ccnt"));
            output.append("\n locx : " + jObj.getDouble("locx"));
            output.append("\n locy : " + jObj.getDouble("locy"));
        }
    }
    catch (JSONException e1)
    {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    tvText.setText(output.toString());