php如何将json数据写入数据库
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php如何将json数据写入数据库相关的知识,希望对你有一定的参考价值。
得到的json为 "angle":221,"prize":"\u4e03\u7b49\u5956","jp":7
我需要把jp的值“7”写入数据库,
请问怎么写代码,感谢
然后从数组里面通过键值(jp)把7拿出来,
最后把7存入到数据库就可以了。追问
能不能给出代码?谢谢
参考技术A 对象json获取对象下标返回json $json=$obj->GetFullStockJSONResult
返回数组 $arr =json_decode($json);
把数组存入数据库
如何将 json 输出保存在 String 对象中。重复数据写入所有拆分的文件中?
【中文标题】如何将 json 输出保存在 String 对象中。重复数据写入所有拆分的文件中?【英文标题】:How to save the json output in a String object.Duplicate data is written in all the splitted files? 【发布时间】:2016-05-03 21:50:16 【问题描述】:我正在拆分一个 Json 文件并将每个 Json 对象写入一个单独的文件,但我想将每个拆分的 Json 对象保存到一个字符串对象。不知道该怎么做。
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.nio.charset.StandardCharsets;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;
public class TweetTestStack
public static void main(String[] args) throws Exception
String filePath = "/home/system5/Videos/Main.json";
try (
//BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(is)))
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), StandardCharsets.UTF_8)))
// BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "UTF-8")))
JSONTokener tokener = new JSONTokener(reader);
JSONArray a = new JSONArray(tokener);
for (int i = 0; i < a.length(); ++i)
flushFile((JSONObject) a.get(0), i);
public static void flushFile(JSONObject obj, int d) throws Exception
try (FileOutputStream output = new FileOutputStream("/home/system5/Music/obj-" + d + ".json");
Writer writer = new OutputStreamWriter(output, "UTF-8"))
obj.write(writer);
示例文件:
["created_at":"Thu Jan 07 11:14:54 +0000 2016","id":685057015547220000,"id_str":"685057015547219968","text":"RT @makeinindia: Calling all techies! Collaborate, brainstorm & solve urban design problems at Hackathon 2016 #MakeInIndia Week https://t.c…","source":"<a href=\"http://twitter.com\" rel=\"nofollow\">Twitter Web Client</a>","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":"id":3479265073,"id_str":"3479265073","name":"dilip bobby","screen_name":"heymailme143","location":"","description":"","url":null,"entities":"description":"urls":[],"protected":false,"followers_count":10,"friends_count":35,"listed_count":3,"created_at":"Mon Sep 07 07:11:18 +0000 2015","favourites_count":1,"utc_offset":-28800,"time_zone":"Pacific Time (US & Canada)","geo_enabled":true,"verified":false,"statuses_count":309,"lang":"en","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"C0DEED","profile_background_image_url":"http://abs.twimg.com/images/themes/theme1/bg.png","profile_background_image_url_https":"https://abs.twimg.com/images/themes/theme1/bg.png","profile_background_tile":false,"profile_image_url":"http://abs.twimg.com/sticky/default_profile_images/default_profile_4_normal.png","profile_image_url_https":"https://abs.twimg.com/sticky/default_profile_images/default_profile_4_normal.png","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"has_extended_profile":false,"default_profile":true,"default_profile_image":true,"following":false,"follow_request_sent":false,"notifications":false,"geo":null,"coordinates":null,"place":null,"contributors":null,"retweeted_status":"created_at":"Sun Dec 20 12:47:16 +0000 2015","id":678557275829592000,"id_str":"678557275829592064","text":"Calling all techies! Collaborate, brainstorm & solve urban design problems at Hackathon 2016 #MakeInIndia Week https://t.co/ojP9Ih4p2Z","source":"<a href=\"http://twitter.com\" rel=\"nofollow\">Twitter Web Client</a>","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":"id":2785480981,"id_str":"2785480981","name":"Make in India","screen_name":"makeinindia","location":"","description":"To register for Make in India Week, click here https://t.co/unsIaezqK6","url":"http://t.co/ZBCKavarN4","entities":"url":"urls":["url":"http://t.co/ZBCKavarN4","expanded_url":"http://www.makeinindia.com","display_url":"makeinindia.com","indices":[0,22]],"description":"urls":["url":"https://t.co/unsIaezqK6","expanded_url":"http://www.makeinindia.com/mumbai-week","display_url":"makeinindia.com/mumbai-week","indices":[47,70]],"protected":false,"followers_count":866474,"friends_count":35,"listed_count":924,"created_at":"Tue Sep 02 07:31:24 +0000 2014","favourites_count":16,"utc_offset":19800,"time_zone":"New Delhi","geo_enabled":true,"verified":true,"statuses_count":4869,"lang":"en","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"FFFFFF","profile_background_image_url":"http://abs.twimg.com/images/themes/theme1/bg.png","profile_background_image_url_https":"https://abs.twimg.com/images/themes/theme1/bg.png","profile_background_tile":false,"profile_image_url":"http://pbs.twimg.com/profile_images/514373412588765184/jQwP6ERL_normal.jpeg","profile_image_url_https":"https://pbs.twimg.com/profile_images/514373412588765184/jQwP6ERL_normal.jpeg","profile_banner_url":"https://pbs.twimg.com/profile_banners/2785480981/1450423946","profile_link_color":"0084B4","profile_sidebar_border_color":"FFFFFF","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":false,"has_extended_profile":false,"default_profile":false,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false,"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"retweet_count":1624,"favorite_count":3168,"entities":"hashtags":["text":"MakeInIndia","indices":[97,109]],"symbols":[],"user_mentions":[],"urls":[],"media":["id":678556556489658400,"id_str":"678556556489658369","indices":[115,138],"media_url":"http://pbs.twimg.com/tweet_video_thumb/CWq3l82UEAEjnrV.png","media_url_https":"https://pbs.twimg.com/tweet_video_thumb/CWq3l82UEAEjnrV.png","url":"https://t.co/ojP9Ih4p2Z","display_url":"pic.twitter.com/ojP9Ih4p2Z","expanded_url":"http://twitter.com/makeinindia/status/678557275829592064/photo/1","type":"photo","sizes":"large":"w":800,"h":800,"resize":"fit","small":"w":340,"h":340,"resize":"fit","thumb":"w":150,"h":150,"resize":"crop","medium":"w":600,"h":600,"resize":"fit"],"extended_entities":"media":["id":678556556489658400,"id_str":"678556556489658369","indices":[115,138],"media_url":"http://pbs.twimg.com/tweet_video_thumb/CWq3l82UEAEjnrV.png","media_url_https":"https://pbs.twimg.com/tweet_video_thumb/CWq3l82UEAEjnrV.png","url":"https://t.co/ojP9Ih4p2Z","display_url":"pic.twitter.com/ojP9Ih4p2Z","expanded_url":"http://twitter.com/makeinindia/status/678557275829592064/photo/1","type":"animated_gif","sizes":"large":"w":800,"h":800,"resize":"fit","small":"w":340,"h":340,"resize":"fit","thumb":"w":150,"h":150,"resize":"crop","medium":"w":600,"h":600,"resize":"fit","video_info":"aspect_ratio":[1,1],"variants":["bitrate":0,"content_type":"video/mp4","url":"https://pbs.twimg.com/tweet_video/CWq3l82UEAEjnrV.mp4"]],"favorited":false,"retweeted":true,"possibly_sensitive":false,"lang":"en","is_quote_status":false,"retweet_count":1624,"favorite_count":0,"entities":"hashtags":["text":"MakeInIndia","indices":[114,126]],"symbols":[],"user_mentions":["screen_name":"makeinindia","name":"Make in India","id":2785480981,"id_str":"2785480981","indices":[3,15]],"urls":[],"media":["id":678556556489658400,"id_str":"678556556489658369","indices":[143,144],"media_url":"http://pbs.twimg.com/tweet_video_thumb/CWq3l82UEAEjnrV.png","media_url_https":"https://pbs.twimg.com/tweet_video_thumb/CWq3l82UEAEjnrV.png","url":"https://t.co/ojP9Ih4p2Z","display_url":"pic.twitter.com/ojP9Ih4p2Z","expanded_url":"http://twitter.com/makeinindia/status/678557275829592064/photo/1","type":"photo","sizes":"large":"w":800,"h":800,"resize":"fit","small":"w":340,"h":340,"resize":"fit","thumb":"w":150,"h":150,"resize":"crop","medium":"w":600,"h":600,"resize":"fit","source_status_id":678557275829592000,"source_status_id_str":"678557275829592064","source_user_id":2785480981,"source_user_id_str":"2785480981"],"extended_entities":"media":["id":678556556489658400,"id_str":"678556556489658369","indices":[143,144],"media_url":"http://pbs.twimg.com/tweet_video_thumb/CWq3l82UEAEjnrV.png","media_url_https":"https://pbs.twimg.com/tweet_video_thumb/CWq3l82UEAEjnrV.png","url":"https://t.co/ojP9Ih4p2Z","display_url":"pic.twitter.com/ojP9Ih4p2Z","expanded_url":"http://twitter.com/makeinindia/status/678557275829592064/photo/1","type":"animated_gif","sizes":"large":"w":800,"h":800,"resize":"fit","small":"w":340,"h":340,"resize":"fit","thumb":"w":150,"h":150,"resize":"crop","medium":"w":600,"h":600,"resize":"fit","source_status_id":678557275829592000,"source_status_id_str":"678557275829592064","source_user_id":2785480981,"source_user_id_str":"2785480981","video_info":"aspect_ratio":[1,1],"variants":["bitrate":0,"content_type":"video/mp4","url":"https://pbs.twimg.com/tweet_video/CWq3l82UEAEjnrV.mp4"]],"favorited":false,"retweeted":true,"possibly_sensitive":false,"lang":"en","created_at":"Thu Jan 07 11:14:23 +0000 2016","id":685056883325972500,"id_str":"685056883325972480","text":"RT @IntelIndia: Grammy winning composer @arrahman uses Intel Curie to turn fingertips into guitars at #CES https://t.co/5NJ3nWofyi https://…","source":"<a href=\"http://twitter.com\" rel=\"nofollow\">Twitter Web Client</a>","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":"id":3479265073,"id_str":"3479265073","name":"dilip bobby","screen_name":"heymailme143","location":"","description":"","url":null,"entities":"description":"urls":[],"protected":false,"followers_count":10,"friends_count":35,"listed_count":3,"created_at":"Mon Sep 07 07:11:18 +0000 2015","favourites_count":1,"utc_offset":-28800,"time_zone":"Pacific Time (US & Canada)","geo_enabled":true,"verified":false,"statuses_count":309,"lang":"en","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"C0DEED","profile_background_image_url":"http://abs.twimg.com/images/themes/theme1/bg.png","profile_background_image_url_https":"https://abs.twimg.com/images/themes/theme1/bg.png","profile_background_tile":false,"profile_image_url":"http://abs.twimg.com/sticky/default_profile_images/default_profile_4_normal.png","profile_image_url_https":"https://abs.twimg.com/sticky/default_profile_images/default_profile_4_normal.png","profile_link_color":"0084B4","profile_sidebar_border_color":"C0DEED","profile_sidebar_fill_color":"DDEEF6","profile_text_color":"333333","profile_use_background_image":true,"has_extended_profile":false,"default_profile":true,"default_profile_image":true,"following":false,"follow_request_sent":false,"notifications":false,"geo":null,"coordinates":null,"place":null,"contributors":null,"retweeted_status":"created_at":"Wed Jan 06 11:23:57 +0000 2016","id":684696902030475300,"id_str":"684696902030475265","text":"Grammy winning composer @arrahman uses Intel Curie to turn fingertips into guitars at #CES https://t.co/5NJ3nWofyi https://t.co/HWyeANow85","source":"<a href=\"http://www.sprinklr.com\" rel=\"nofollow\">Sprinklr</a>","truncated":false,"in_reply_to_status_id":null,"in_reply_to_status_id_str":null,"in_reply_to_user_id":null,"in_reply_to_user_id_str":null,"in_reply_to_screen_name":null,"user":"id":45800443,"id_str":"45800443","name":"Intel India","screen_name":"IntelIndia","location":"India","description":"Intel India’s official Twitter handle – For technologies and devices that enrich people's lives; House Rules: https://t.co/k0OmxmHmeO","url":"http://t.co/BnWL5Xl2Kp","entities":"url":"urls":["url":"http://t.co/BnWL5Xl2Kp","expanded_url":"http://www.intel.in","display_url":"intel.in","indices":[0,22]],"description":"urls":["url":"https://t.co/k0OmxmHmeO","expanded_url":"https://tr.im/IoBoX","display_url":"tr.im/IoBoX","indices":[112,135]],"protected":false,"followers_count":77465,"friends_count":549,"listed_count":311,"created_at":"Tue Jun 09 09:13:21 +0000 2009","favourites_count":3321,"utc_offset":19800,"time_zone":"New Delhi","geo_enabled":true,"verified":true,"statuses_count":12901,"lang":"en","contributors_enabled":false,"is_translator":false,"is_translation_enabled":false,"profile_background_color":"FFFFFF","profile_background_image_url":"http://pbs.twimg.com/profile_background_images/378800000172020428/8hBrBebn.jpeg","profile_background_image_url_https":"https://pbs.twimg.com/profile_background_images/378800000172020428/8hBrBebn.jpeg","profile_background_tile":false,"profile_image_url":"http://pbs.twimg.com/profile_images/685423865598619652/Cqe40-1O_normal.jpg","profile_image_url_https":"https://pbs.twimg.com/profile_images/685423865598619652/Cqe40-1O_normal.jpg","profile_banner_url":"https://pbs.twimg.com/profile_banners/45800443/1438925471","profile_link_color":"0070C5","profile_sidebar_border_color":"000000","profile_sidebar_fill_color":"86C6E3","profile_text_color":"444444","profile_use_background_image":true,"has_extended_profile":false,"default_profile":false,"default_profile_image":false,"following":false,"follow_request_sent":false,"notifications":false,"geo":null,"coordinates":null,"place":null,"contributors":null,"is_quote_status":false,"retweet_count":1822,"favorite_count":2653,"entities":"hashtags":["text":"CES","indices":[86,90]],"symbols":[],"user_mentions":["screen_name":"arrahman","name":"A.R.Rahman","id":19895282,"id_str":"19895282","indices":[24,33]],"urls":["url":"https://t.co/5NJ3nWofyi","expanded_url":"http://www.intel.sg/ces","display_url":"intel.sg/ces","indices":[91,114]],"media":["id":684696901934006300,"id_str":"684696901934006272","indices":[115,138],"media_url":"http://pbs.twimg.com/media/CYCINGKWQAAtt6z.jpg","media_url_https":"https://pbs.twimg.com/media/CYCINGKWQAAtt6z.jpg","url":"https://t.co/HWyeANow85","display_url":"pic.twitter.com/HWyeANow85","expanded_url":"http://twitter.com/IntelIndia/status/684696902030475265/photo/1","type":"photo","sizes":"medium":"w":599,"h":337,"resize":"fit","small":"w":340,"h":191,"resize":"fit","thumb":"w":150,"h":150,"resize":"crop","large":"w":599,"h":337,"resize":"fit"],"extended_entities":"media":["id":684696901934006300,"id_str":"684696901934006272","indices":[115,138],"media_url":"http://pbs.twimg.com/media/CYCINGKWQAAtt6z.jpg","media_url_https":"https://pbs.twimg.com/media/CYCINGKWQAAtt6z.jpg","url":"https://t.co/HWyeANow85","display_url":"pic.twitter.com/HWyeANow85","expanded_url":"http://twitter.com/IntelIndia/status/684696902030475265/photo/1","type":"photo","sizes":"medium":"w":599,"h":337,"resize":"fit","small":"w":340,"h":191,"resize":"fit","thumb":"w":150,"h":150,"resize":"crop","large":"w":599,"h":337,"resize":"fit"],"favorited":false,"retweeted":true,"possibly_sensitive":false,"lang":"en","is_quote_status":false,"retweet_count":1822,"favorite_count":0,"entities":"hashtags":["text":"CES","indices":[102,106]],"symbols":[],"user_mentions":["screen_name":"IntelIndia","name":"Intel India","id":45800443,"id_str":"45800443","indices":[3,14],"screen_name":"arrahman","name":"A.R.Rahman","id":19895282,"id_str":"19895282","indices":[40,49]],"urls":["url":"https://t.co/5NJ3nWofyi","expanded_url":"http://www.intel.sg/ces","display_url":"intel.sg/ces","indices":[107,130]],"media":["id":684696901934006300,"id_str":"684696901934006272","indices":[139,140],"media_url":"http://pbs.twimg.com/media/CYCINGKWQAAtt6z.jpg","media_url_https":"https://pbs.twimg.com/media/CYCINGKWQAAtt6z.jpg","url":"https://t.co/HWyeANow85","display_url":"pic.twitter.com/HWyeANow85","expanded_url":"http://twitter.com/IntelIndia/status/684696902030475265/photo/1","type":"photo","sizes":"medium":"w":599,"h":337,"resize":"fit","small":"w":340,"h":191,"resize":"fit","thumb":"w":150,"h":150,"resize":"crop","large":"w":599,"h":337,"resize":"fit","source_status_id":684696902030475300,"source_status_id_str":"684696902030475265","source_user_id":45800443,"source_user_id_str":"45800443"],"extended_entities":"media":["id":684696901934006300,"id_str":"684696901934006272","indices":[139,140],"media_url":"http://pbs.twimg.com/media/CYCINGKWQAAtt6z.jpg","media_url_https":"https://pbs.twimg.com/media/CYCINGKWQAAtt6z.jpg","url":"https://t.co/HWyeANow85","display_url":"pic.twitter.com/HWyeANow85","expanded_url":"http://twitter.com/IntelIndia/status/684696902030475265/photo/1","type":"photo","sizes":"medium":"w":599,"h":337,"resize":"fit","small":"w":340,"h":191,"resize":"fit","thumb":"w":150,"h":150,"resize":"crop","large":"w":599,"h":337,"resize":"fit","source_status_id":684696902030475300,"source_status_id_str":"684696902030475265","source_user_id":45800443,"source_user_id_str":"45800443"],"favorited":false,"retweeted":true,"possibly_sensitive":false,"lang":"en"]
【问题讨论】:
你可以通过obj.toString()
获取JSON字符串。
这有帮助吗? ***.com/questions/216894/… 使用 ByteArrayOutputStream 而不是 FileOutputStream
【参考方案1】:
只是一个小错误,把0改成i即可;(分割后的文件没有发现重复)
for (int i = 0; i < a.length(); ++i)
flushFile((JSONObject) a.get(i), i);
并将每个分割的 json 保存在一个字符串中:执行以下从 void 到 String 的更改。
public static String flushFile(JSONObject obj, int d) throws Exception
String s=obj.toString();
return s;
【讨论】:
以上是关于php如何将json数据写入数据库的主要内容,如果未能解决你的问题,请参考以下文章