Showing posts with label JSON. Show all posts
Showing posts with label JSON. Show all posts

Java: JSON

In this tutorial we will parse Flickr JSON


https://api.flickr.com/services/feeds/photos_public.gne?tags=mountains&tagmode=all&format=json#








As an Amazon Associate I earn from qualifying purchases.

Java: JSON

In this tutorial we will parse Flickr JSON


https://api.flickr.com/services/feeds/photos_public.gne?tags=mountains&tagmode=all&format=json#








As an Amazon Associate I earn from qualifying purchases.

Java: read URL to String text

The blog post describes a method for reading data from a URL in Java.
It explains how to establish a stream connection, read data into a buffer, and convert that data into a String format.

The method is particularly useful for developers needing to fetch web data in Java applications.

I was using it here to retrieve a JSON feed from Flickr.





As an Amazon Associate I earn from qualifying purchases.

JSON vs. XML

Comparison of JSON and XML syntax (from http://json.org/example)





As an Amazon Associate I earn from qualifying purchases.

JSON vs. XML

Comparison of JSON and XML syntax (from http://json.org/example)





As an Amazon Associate I earn from qualifying purchases.

12a. Android: JSON OpenWeatherMap

In this tutorial we will implement basic weather OpenWeatherMap API using JSON (JavaScript Object Notation).


As an Amazon Associate I earn from qualifying purchases.

12a. Android: JSON OpenWeatherMap

In this tutorial we will implement basic weather OpenWeatherMap API using JSON (JavaScript Object Notation).
links:

API information:
http://openweathermap.org/api

JSON:
http://api.openweathermap.org/data/2.5/weather?q=Mundelein,IL

Image URL:
http://openweathermap.org/img/w/10d.png

JSON (pretty) formatter in TextWrangler editor (Mac)
http://ukitech.blogspot.com/2012/08/format-json-in-free-textwrangler.html

Example JSON data:

http://api.openweathermap.org/data/2.5/weather?q=Mundelein,IL
http://openweathermap.org/img/w/10d.png

{
  "base": "cmc stations",
  "clouds": {
    "all": 1
  },
  "cod": 200,
  "coord": {
    "lat": 42.27,
    "lon": -88
  },
  "dt": 1416040500,
  "id": 4903184,
  "main": {
    "humidity": 92,
    "pressure": 1027,
    "temp": 265.36,
    "temp_max": 268.15,
    "temp_min": 261.15
  },
  "name": "Mundelein",
  "sys": {
    "country": "United States of America",
    "id": 2981,
    "message": 0.0294,
    "sunrise": 1416055401,
    "sunset": 1416090602,
    "type": 1
  },
  "weather": [
    {
      "description": "sky is clear",
      "icon": "01n",
      "id": 800,
      "main": "Clear"
    }
  ],
  "wind": {
    "deg": 274.501,
    "speed": 4.54
  }
}

Step: Create a new Android module

  • File > New Module...
  • Android Application
  • Application Name: Weather Station
  • Module Name: appWeatherStationJson
  • Package Name: com.yourname.android.app.json.weather
  • Min SDK: 8
  • Target SDK: 19 (KitKat 4.4)
  • Compile with: 21
  • Java Lang: 6.0
  • Theme: Holo Light
  • Create Activity
  • Suport mode: Fragments
  • Suport mode: Action Bar


Next
  • Blank Activity
Next
  • MainActivity
  • activity_main
Finish


Step: add INTERNET permission

We need to add INTERNET permission since we will be accessing JSON over HTTP

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="com.chicagoandroid.android.app.json.weather">
    <application
            android:allowBackup="true"
            android:icon="@drawable/ic_launcher"
            android:label="@string/app_name"
            android:theme="@style/AppTheme">
        <activity
                android:name=".MainActivity"
                android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.INTERNET"/>
</manifest>



As an Amazon Associate I earn from qualifying purchases.

Format JSON in free TextWrangler

Here is an easy way to get formatted JSON in FREE TextWrangler:

Create text (Python) file:
~/Library/Application\ Support/TextWrangler/Text\ Filters/Format\ JSON.py




#!/usr/local/bin/python
import fileinput
import json
if __name__ == "__main__":
  text = ''
  for line in fileinput.input():
    text = text + ' ' + line.strip()    
  jsonObj = json.loads(text)  
  print json.dumps(jsonObj, sort_keys=True, indent=2) 



or shorter:


#!/usr/local/bin/python
import fileinput
import json
print json.dumps( json.loads(''.join([line.strip() for line in fileinput.input()])), sort_keys=True, indent=2)




Save it and use it!









It take time and effort to create tutorials, please support my efforts with a couple dollar donation, any amount will be greatly appreciated!



As an Amazon Associate I earn from qualifying purchases.

Format JSON in free TextWrangler

Here is an easy way to get formatted JSON in FREE TextWrangler:

Create text (Python) file:
~/Library/Application\ Support/TextWrangler/Text\ Filters/Format\ JSON.py




#!/usr/local/bin/python
import fileinput
import json
if __name__ == "__main__":
  text = ''
  for line in fileinput.input():
    text = text + ' ' + line.strip()    
  jsonObj = json.loads(text)  
  print json.dumps(jsonObj, sort_keys=True, indent=2) 



or shorter:


#!/usr/local/bin/python
import fileinput
import json
print json.dumps( json.loads(''.join([line.strip() for line in fileinput.input()])), sort_keys=True, indent=2)




Save it and use it!









It take time and effort to create tutorials, please support my efforts with a couple dollar donation, any amount will be greatly appreciated!



As an Amazon Associate I earn from qualifying purchases.

XML file design and formatting

It is well know that XML files are larger and therefore slower that JSON, however with careful design they don't have to be so. Remember the rule:

1) it the tag repeats only once then it should be converted to an attribute, for example:
- name, latitude, longitude, etc.

2) shorten the tag names, but don't go to far so it is still human readable
3) compress (zip) XML files when transferring them over a network



Formatting:
1) put each attribute on separate line, white space does not cost when compressed
2) extend line length to at least 120 characters for readability, your window size most likely allows for more






As an Amazon Associate I earn from qualifying purchases.

XML file design and formatting

It is well know that XML files are larger and therefore slower that JSON, however with careful design they don't have to be so. Remember the rule:

1) it the tag repeats only once then it should be converted to an attribute, for example:
- name, latitude, longitude, etc.

2) shorten the tag names, but don't go to far so it is still human readable
3) compress (zip) XML files when transferring them over a network



Formatting:
1) put each attribute on separate line, white space does not cost when compressed
2) extend line length to at least 120 characters for readability, your window size most likely allows for more






As an Amazon Associate I earn from qualifying purchases.

apt quotation..