如何从JSON中选择一些数据

时间:2015-10-30 08:47:30

标签: json

请仔细阅读以下json数据

{
"response": {
    "status": 1,
    "httpStatus": 200,
    "data": {
      "675": {
        "Offer": {
          "name": Vivek
        }
      },
      "787": {
        "Offer": {
          "name": Aashiq
        }
      }
    },
    "errors": [],
    "errorMessage": null
  }
}

我想选择整数675& 787从上面的json数据和使用php插入Mysql目前我使用的是下面的php代码。帮我选择这个整数并谢谢。

<?php
include 'init.php';
include 'db.php';

$json = {
"response": {
    "status": 1,
    "httpStatus": 200,
    "data": {
      "675": {
        "Offer": {
          "name": Vivek
        }
      },
      "787": {
        "Offer": {
          "name": Aashiq
        }
      }
    },
    "errors": [],
    "errorMessage": null
  }
};


$mydata = json_decode($json, true);

////mysql///
foreach ($mydata["response"]["data"] as $key => $value)
    echo $key;

mysql_query("INSERT INTO due
    (integer) VALUES('$key')")
    or die(mysql_error());
?>

2 个答案:

答案 0 :(得分:-1)

https://stackoverflow.com/a/2892156/5445351功能可以帮助您:

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        >
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:text="button"/>
    </LinearLayout>
</ScrollView>

另见json_decode

答案 1 :(得分:-1)

您可以尝试以下方式通过JQuery

阅读
var data={
"response": {
    "status": 1,
    "httpStatus": 200,
    "data": {
      "675": {
        "Offer": {
          "name": "Vivek"
        }
      },
      "787": {
        "Offer": {
          "name": "Aashiq"
        }
      }
    },
    "errors": [],
    "errorMessage": null
  }
};

var innerData = data.response.data;

jQuery.each(innerData,function(key,value)
{
    console.log(key);
});