每天推薦一個 GitHub 優質開源項目和一篇精選英文科技或編程文章原文,歡迎關注開源日報。交流QQ群:202790710;電報群 https://t.me/OpeningSourceOrg


今日推薦開源項目:《幾分鐘初步認識JSON

推薦理由:本文旨在短時間內讓讀者簡單的認識 JSON

JSON 簡介

JSON,即 JavaScript Object Notation 的縮寫,指的是 JavaScript 的對象表示法,雖然 JSON 使用 JS 語法來描述數據對象,但其本身獨立於語言和平台,JSON 的解析器和庫都支持不同的編程語言。

與 XML(可擴展標記語言)類似的是,JSON 是純文本;具有自我描述性,即我們可以簡單的理解它;具有層級結構。

JSON 語法

JSON 數據的書寫格式是名稱/值對,其中包括雙引號中的欄位名稱,後面寫一個冒號,然後是值,值可以是整數或浮點數、雙引號中的字元串、true or false、方括弧中的數組、花括弧中的對象;null。例如下面這條

"JSON":"helloworld"

這條語句等價於

JSON = "helloworld"

 

JSON 對象在花括弧中書寫,其中可以包含多個名稱/值對,比如下面這條

{
  "JS":"hello",
  "ON":"world"
}

等價於

JS ="hello", ON ="world"

 

JSON 數組在方括弧中書寫,其中可以包含多個對象,

{
 "JP":[
    {"JS":"hell", "ON":"wrld"},
    {"JS":"helo", "ON":"wold"},
    {"JS":"hllo", "ON":"word"}
  ]
}

因為 JSON 使用的是 JavaScript 的語法,所以無需額外的軟體就能處理JS 中的 JSON,比如這樣

var JP = [
  {"JS":"hell", "ON":"wrld"},
  {"JS":"helo", "ON":"wold"},
  {"JS":"hllo", "ON":"word"}
];

如果要訪問對象數組,可以像這樣進行訪問

JP[0].JS

返回的內容是

hell

JavaScript 處理 JSON 數據

JSON 最常見的用法之一,是從 web 伺服器上讀取 JSON 數據,將 JSON 數據轉換為 JavaScript 對象,然後在網頁中使用該數據。接下來,我們來討論 JavaScript 如何處理 JSON 數據。

在 web 頁面中用 JavaScript 處理 JSON 數據時,

function handleJson() {
 var Message={"name":"Michael","address":
     {"city":"Beijing","street":"Chaoyang Road","postcode":100025}
 };
 document.write(Message.name);
 document.write(Message.address.city);
}

假定伺服器返回的 JSON 數據是上文的:

{
  "name":"Michael",
  "address":{
    "city":"Beijing",
    "street":" Chaoyang Road ",
    "postcode":100025
  }
}

只需將其賦值給一個 JavaScript 變數(例如上文中的 Message),就可以立刻使用該變數並更新頁面中的信息了。

這相比起 XML 就顯得更加的容易。我們要做的僅僅是將伺服器返回的JSON 數據賦給一個 JavaScript 變數即可。

JSON 的數據可移植性問題

JSON 允許 Unicode 行結束符 U + 2028(行分隔符)和 U + 2029(段落分隔符)在引用字元串中顯示為非轉義,而 JavaScript 不顯示。這是JSON 只禁止"控制字元"的結果。為了最大限度地提高可移植性,這些字元應該反斜杠轉義。生成 JSONP 時,這個微妙之處非常重要。

更多關於 U+2028/2029 的討論:

https://github.com/rack/rack-contrib/pull/37

JSON 文檔可以用 UTF-8,UTF-16 或 UTF-32 編碼,默認編碼為 UTF-8(I-JSON 僅支持 UTF-8)。這些編碼支持完整的 Unicode 字符集,包括基本多語言面以外的字元(U + 10000至U + 10FFFF)。但是,如果轉義,那麼這些字元必須使用 UTF-16 代理對(一些 JSON 解析器遺漏的細節)編寫。例如,在 JSON 中包含表情符號字元U + 1F602 ?(喜極而泣):

{ "face" : "?" }

//或

{ "face" : "\uD83D\uDE02" }

JSON 中的數字在編程語言中的表示是不可知的。整數和浮點值之間沒有區分太清楚:一些可能把42,42.0以及4.2E+1當成是一樣的,而其他的可能不會。對於溢出,下溢,精度損失或舍入等問題沒有要求。另外,JSON 沒有提及有符號零的處理:0.0是否不同於-0.0。使用 IEEE 754浮點標準(包括 JavaScript)的大多數實現都保留帶符號的零; 但並不是所有的 JSON 實現都可以這樣做。


今日推薦英文原文:《Python Top 10 Open Source Projects (v.Mar 2018)》作者:Mybridge

原文鏈接:https://medium.mybridge.co/python-top-10-open-source-projects-v-mar-2018-e2ce1d645ec

Python Top 10 Open Source Projects (v.Mar 2018)

For the past month, we ranked nearly 250 Python Open Source Projects to pick the Top 10.

We compared projects with new or major release during this period. Mybridge AI ranks projects based on a variety of factors to measure its quality for professionals.

  • Average number of Github stars in this edition: 570⭐️
  • 「Watch」 Python Top 10 Open Source on Github and get email once a month.
  • Topics: Datetime, Debugger, Maps, Data validation, Print, Trading Bot, Multidiff, Matrix, Downloder

Open source projects can be useful for programmers. Hope you find an interesting project that inspires you.

Rank 1

Delorean: Delorean: Time Travel Made Easy [V 1.0] [1335 stars on Github]. Courtesy of Mahdi Yusuf


Rank 2

Birdseye: Quick, convenient, expression-centric, graphical Python debugger using the AST [674 stars on Github]. Courtesy of Alex Hall


Rank 3

Som-tsp: Solving the Traveling Salesman Problem using Self-Organizing Maps [432 stars on Github]. Courtesy of Diego Vicente


Rank 4

Voluptuous: Voluptuous, despite the name, is a Python data validation library. [1066 stars on Github]. Courtesy of Alec Thomas


Rank 5

Icecream: Sweet and Creamy Print Debugging [530 stars on Github]. Courtesy of Gruns


Rank 6

Binance-trader: Cryptocurrency Trading Bot for Binance (Experimental) [590 stars on Github]. Courtesy of Yasin


Rank 7

Multidiff: Binary data diffing for multiple objects or streams of data [188 stars on Github]. Courtesy of Juha Kivekäs


Rank 8

Unimatrix: Python script to simulate the display from 「The Matrix」 in terminal. Uses half-width katakana unicode characters by default, but can use custom character sets. Accepts keyboar… [558 stars on Github]. Courtesy of Will8211


Rank 9

Mypy-protobuf: open source tools to generate mypy stubs from protobufs [33 stars on Github]. Courtesy of Dropbox


Rank 10

Lulu: A friendly you-get fork (⏬ Dumb downloader that scrapes the web) [296 stars on Github].

 


每天推薦一個 GitHub 優質開源項目和一篇精選英文科技或編程文章原文,歡迎關注開源日報。交流QQ群:202790710;電報群 https://t.me/OpeningSourceOrg