开发者

Update mysql table using data from json file

I am complete new to json and thefore struggling to work with.

I have a list of id's stored in a id field of a mysql table.

For example id field contai开发者_如何学编程ns these values

  1. 100001
  2. 100002
  3. 100003

The json file containes properties of these id's, like this.

{"100001":{"person":{"name":"John","age":"32,"address":"123 street"}},
{"100002":{"person":{"name":"jenny","age":"22,"address":"100 street"}},
{"100003":{"person":{"name":"james","age":"25,"address":"200 street"}}

My intention is to update the name, age and address fields in the mysql table with the data in json file using the id as a conditon in the where clause.

But I have no idea where to even start.

Can someone who know's what they doing, please direct me.

Thanks in advance.


what you struggling to understand? json_decode() http://php.net/manual/en/function.json-decode.php

$var = '{"100001":{"person":{"name":"John","age":"32,"address":"123 street"}},
{"100002":{"person":{"name":"jenny","age":"22,"address":"100 street"}},
{"100003":{"person":{"name":"james","age":"25,"address":"200 street"}}';

print_r(json_decode($var));

that will give you an array you can either foreach though and create separate update statements of build one large update statement and execute it.


You SHOULD be getting an error somewhere; your curly braces don't match and you're missing closing quotes after the age:

{"100001":{"person":{"name":"John","age":"32,"address":"123 street"}},

should be

{"100001":{"person":{"name":"John","age":"32","address":"123 street"}}},

You're missing the closing brace for the actual ID. To make it easier to read, try formatting like this:

{
 "100001":{
           "person":{
                     "name":"John",
                     "age":"32",
                     "address":"123 street"
                     }
           }
 },
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜