├── _config.yml ├── LICENSE ├── _layouts └── default.html └── README.md /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-cayman 2 | title: JSON GUIDE 3 | description: For those who wanna learn JSON 4 | show_downloads: true 5 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Tanbir chowdhury 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /_layouts/default.html: -------------------------------------------------------------------------------- 1 | 2 | 3 |
4 | 5 | 6 | {% seo %} 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |" + nesarr.computer[i].name + "
"; 156 | for (j in nesarr.computer[i].models) { 157 | x += nesarr.computer[i].models[j]; 158 | } 159 | } 160 | ``` 161 | ### Json Modify array Values: 162 | ```js 163 | nesarr.computer[1] = "XPS 17" 164 | ``` 165 | ### Json Delete array: 166 | ```js 167 | delete nesarr.computer[1]; 168 | ``` 169 | ## Json parse 170 | --- 171 | ### Json Parsing 172 | ```js 173 | '{ "name":"Rafi", "Phone":01723, "city":"Dhaka"}' 174 | ``` 175 | ### Use the JavaScript function Json parse 176 | ```js 177 | var json = '{"name":"rafi", "result":true,"count":2}', 178 | obj = JSON.parse(json); 179 | ``` 180 | > Use the JavaScript object 181 | ```js 182 | 183 | 186 | ``` 187 | ### get data from the server: 188 | ```js 189 | var xmlhttp = new XMLHttpRequest(); 190 | xmlhttp.onreadystatechange = function() { 191 | if (this.readyState == 4 && this.status == 200) { 192 | var myObj = JSON.parse(this.responseText); 193 | document.getElementById("demo").innerHTML = obj.name; 194 | } 195 | }; 196 | xmlhttp.open("GET", "json_demo.txt", true); 197 | xmlhttp.send(); 198 | ``` 199 | ## Json stringify 200 | --- 201 | ### Json Stringify with JavaScript Object: 202 | ```js 203 | var myobj = { "name":"Rafi", "hometown":"Barisal", "currentcity":"Dhaka", "phone":01723****}; 204 | ``` 205 | > JavaScript function JSON.stringify, and string data sent to be a server 206 | ```js 207 | var myJSON = JSON.stringify(myobj); 208 | document.getElementById("demo").innerHTML = myJSON; 209 | ``` 210 | ### Json Stringify date: 211 | ```js 212 | var myobj = { "name":"Rafi", "date":new Date(), "home":"Barisal" }; 213 | ``` 214 | ### Json Stringify function: 215 | ```js 216 | var myobj = { "name":"Rafi", "phone":function () {return 01723***;}, "home":"Barisal" }; 217 | ``` 218 | ## JSON Schema 219 | --- 220 | ### Building a product schema: 221 | > JSON data for a product API 222 | ```js 223 | { 224 | "id": 1, 225 | "name": "Ace Plus", 226 | "price": 12.50, 227 | "tags": ["medicine", "drug"] 228 | } 229 | ``` 230 | > Starting the schema 231 | ```js 232 | { 233 | "$schema": "http://json-schema.org/draft-06/schema#", 234 | "title": "Product", 235 | "description": "A product from Medicine", 236 | "type": "medicine" 237 | } 238 | ``` 239 | ### Defining the properties: 240 | > what is id? 241 | ```js 242 | { 243 | "$schema": "http://json-schema.org/draft-06/schema#", 244 | "title": "Product", 245 | "description": "A product from Medicine", 246 | "type": "object", 247 | "properties": { 248 | "id": { 249 | "description": "The unique indentifier for a product", 250 | "type": "integer" 251 | } 252 | }, 253 | "required": ["id"] 254 | } 255 | ``` 256 | > Is name required? 257 | ```js 258 | { 259 | "$schema": "http://json-schema.org/draft-06/schema#", 260 | "title": "Product", 261 | "description": "A product from Medicine", 262 | "type": "object", 263 | "properties": { 264 | "id": { 265 | "description": "The unique identifier for a product", 266 | "type": "integer" 267 | }, 268 | "name": { 269 | "description": "Name of the product", 270 | "type": "string" 271 | }, 272 | "required": ["id","name"] 273 | } 274 | ``` 275 | > Can price be 0? 276 | ```js 277 | { 278 | "$schema": "http://json-schema.org/draft-06/schema#", 279 | "title": "Product", 280 | "description": "A product from Acme's catalog", 281 | "type": "object", 282 | "properties": { 283 | "id": { 284 | "description": "The unique identifier for a product", 285 | "type": "integer" 286 | }, 287 | "name": { 288 | "description": "Name of the product", 289 | "type": "string" 290 | }, 291 | "price": { 292 | "type": "number", 293 | "exclusiveMinimum": 0 294 | } 295 | }, 296 | "required": ["id", "name", "price"] 297 | } 298 | ``` 299 | > Are all tags strings? 300 | ```js 301 | { 302 | "$schema": "http://json-schema.org/draft-06/schema#", 303 | "title": "Product", 304 | "description": "A product from Acme's catalog", 305 | "type": "object", 306 | "properties": { 307 | "id": { 308 | "description": "The unique identifier for a product", 309 | "type": "integer" 310 | }, 311 | "name": { 312 | "description": "Name of the product", 313 | "type": "string" 314 | }, 315 | "price": { 316 | "type": "number", 317 | "exclusiveMinimum": 0 318 | }, 319 | "tags": { 320 | "type": "array", 321 | "items": { 322 | "type": "string" 323 | }, 324 | "minItems": 1, 325 | "uniqueItems": true 326 | } 327 | }, 328 | "required": ["id", "name", "price"] 329 | } 330 | ``` 331 | > Summary of products: 332 | ```js 333 | [ 334 | { 335 | "id": 2, 336 | "name": "An ice sculpture", 337 | "price": 12.50, 338 | "tags": ["cold", "ice"], 339 | "dimensions": { 340 | "length": 7.0, 341 | "width": 12.0, 342 | "height": 9.5 343 | }, 344 | "warehouseLocation": { 345 | "latitude": -78.75, 346 | "longitude": 20.4 347 | } 348 | }, 349 | { 350 | "id": 3, 351 | "name": "A blue mouse", 352 | "price": 25.50, 353 | "dimensions": { 354 | "length": 3.1, 355 | "width": 1.0, 356 | "height": 1.0 357 | }, 358 | "warehouseLocation": { 359 | "latitude": 54.4, 360 | "longitude": -32.7 361 | } 362 | } 363 | ] 364 | ``` 365 | ## Json-Examples 366 | --- 367 | ### JSON with Php 368 | > encode 369 | ```js 370 | $myArray = array('Name' => 'Tanbir', 'Age' => 22, 'City' => 'Dhaka', 'Job' => 'nai'); 371 | echo json_encode($myArray); 372 | ``` 373 | > decode 374 | ```js 375 | $json = '{"Name": "Tanbir","Age": 22,"City": "Dhaka","Job": "nai"}'; 376 | var_dump(json_decode($json)); 377 | ``` 378 | ### JSON with Python 379 | > encode 380 | ```js 381 | data = [ { 'Name' : 'Rafi', 'Age' : 8, 'City' : 'Dhaka', 'Job' : 'nai'} ] 382 | json = demjson.encode(data) 383 | print json 384 | ``` 385 | > decode 386 | ```js 387 | json = '{"html":5,"css":3,"laravel":5.5}'; 388 | text = demjson.decode(json) 389 | print text 390 | ``` 391 | ### JSON with Java 392 | 393 | > encode 394 | ```js 395 | class JsonEncodeDemo { 396 | 397 | public static void main(String[] args){ 398 | JSONObject obj = new JSONObject(); 399 | 400 | obj.put("name", "rafi"); 401 | obj.put("number", new Integer(01723)); 402 | obj.put("balance", new Double(123450.21)); 403 | obj.put("ip", new Boolean(true)); 404 | 405 | System.out.print(obj); 406 | } 407 | } 408 | ``` 409 | > decode 410 | ```js 411 | String jsonString = "{\"stat\":{\"sdr\": \"aa:bb:cc:dd:ee:ff\",\"rcv\": \"aa:bb:cc:dd:ee:ff\",\"time\": \"UTC in millis\",\"type\": 1,\"subt\": 1,\"argv\": [{1,2},{2,3}]}}"; 412 | JSONObject jsonObject = new JSONObject(jsonString); 413 | JSONObject newJSON = jsonObject.getJSONObject("stat"); 414 | System.out.println(newJSON); 415 | ``` 416 | 417 | ### License 418 |  419 | 420 | ### Admin 421 | [Tanbir](https://github.com/totanbir) 422 | 423 | ### Contributors 424 | [contributors](https://github.com/totanbir/json-guide/graphs/contributors) 425 | --------------------------------------------------------------------------------