JsonProperty example


package nl.kameroom.searchservice;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.codehaus.jackson.annotate.JsonProperty;

/**
 * The data tables response.
 */
@SuppressWarnings("serial")
public class DataTablesResponse <T> implements Serializable {
 
 @JsonProperty(value = "sEcho")
 public int echo;
 @JsonProperty(value = "iTotalRecords")
 public int totalRecords;
 @JsonProperty(value = "iTotalDisplayRecords")
 public int totalDisplayRecords;
 @SuppressWarnings("unused")
 @JsonProperty(value = "messages")
 private String[] messages;
 @JsonProperty(value = "aaData")
 public List<T> aaData = new ArrayList<T>();

 public void setMessages(String[] messages) {
 this.messages = messages;
 }
 
}

Python JSON parsing example



#Created on Jul 30, 2013
#@author: tunatore

import json

# you can create a json structure
pythonStructure = { 'JavaLevel':'Expert', 'PythonLevel':"Beginner", 'C#Level':"Good" }

print ("Python Structure = " , pythonStructure)

#dumps will load python structure as json
print ("JSON Converted Structure = ", json.dumps(pythonStructure))
print ("JSON Converted Structure Sorted Keys = " , json.dumps(pythonStructure, sort_keys=True))

#pretty printed JSON
print ("pretty printed JSON \n")
print (json.dumps(pythonStructure, sort_keys=True, indent=5, separators=(',', ' : ')))

#dumps will load json structure as python structure
print ("Python Structure = " , (json.loads(json.dumps(pythonStructure))))

#another python struct conversion

pythonData = dict(
 key1 = "Val 1",
 key2 = "Val 2",
 key3 = "Val 3",
 key4 = 4
)

print ("JSON Converted Structure = ", json.dumps(pythonData,sort_keys=True))
#loading and parsing JSON data from url
import urllib.request
url = "http://carma.org/api/1.1/searchLocations?name=Idaho"
request = urllib.request.Request(url)
response = urllib.request.urlopen(request)
loadJSON = json.loads(response.read().decode('utf-8'))
print (json.dumps(loadJSON, indent=4, sort_keys=True))

# get carbon JSON item
carbon = loadJSON[0]['carbon']
print ("carbon future" , carbon['future'])
print ("carbon past" , carbon['past'])
print ("carbon present" , carbon['present'])

Output;

Python Structure = {‘PythonLevel’: ‘Beginner’, ‘C#Level’: ‘Good’, ‘JavaLevel’: ‘Expert’}
JSON Converted Structure = {“PythonLevel”: “Beginner”, “C#Level”: “Good”, “JavaLevel”: “Expert”}
JSON Converted Structure Sorted Keys = {“C#Level”: “Good”, “JavaLevel”: “Expert”, “PythonLevel”: “Beginner”}
pretty printed JSON

{
“C#Level” : “Good”,
“JavaLevel” : “Expert”,
“PythonLevel” : “Beginner”
}
Python Structure = {‘PythonLevel’: ‘Beginner’, ‘C#Level’: ‘Good’, ‘JavaLevel’: ‘Expert’}
JSON Converted Structure = {“key1”: “Val 1”, “key2”: “Val 2”, “key3”: “Val 3”, “key4”: 4}
[
{
“carbon”: {
“future”: “2068400.0000”,
“past”: “758970.0000”,
“present”: “838420.0000”
},
“country”: {
“id”: “6252001”,
“name”: “United States”
},
“energy”: {
“future”: “18787000.0000”,
“past”: “10677000.0000”,
“present”: “13059000.0000”
},
“fossil”: {
“future”: “0.2371”,
“past”: “0.1666”,
“present”: “0.1339”
},
“hydro”: {
“future”: “0.5648”,
“past”: “0.7809”,
“present”: “0.7996”
},
“id”: “5596512”,
“intensity”: {
“future”: “110.0000”,
“past”: “71.1000”,
“present”: “64.2000”
},
“name”: “Idaho”,
“nuclear”: {
“future”: “0.0000”,
“past”: “0.0000”,
“present”: “0.0000”
},
“plant_count”: “92”,
“renewable”: {
“future”: “0.1981”,
“past”: “0.0525”,
“present”: “0.0664”
},
“type”: “province”
}
]
carbon future 2068400.0000
carbon past 758970.0000
carbon present 838420.0000