Site Github.com Auto Generate Json File By Key
Site Github.com Auto Generate Json File By Key 8,3/10 1368 votes
jsonTopdf.java
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together. Sign up Read from and write to a JSON file, minimizing diffs and preserving formatting. Export JSON to CSV file using Javascript. GitHub Gist: instantly share code, notes, and snippets.
packagebravado; |
importjava.io.ByteArrayOutputStream; |
importjava.io.File; |
importjava.io.FileOutputStream; |
importjava.io.FileWriter; |
importjava.io.IOException; |
importjava.text.SimpleDateFormat; |
importorg.json.JSONArray; |
importorg.json.JSONException; |
importorg.json.JSONObject; |
importcom.itextpdf.text.Document; |
importcom.itextpdf.text.DocumentException; |
importcom.itextpdf.text.PageSize; |
importcom.itextpdf.text.Paragraph; |
importcom.itextpdf.text.Rectangle; |
importcom.itextpdf.text.pdf.PdfWriter; |
publicclassPDFGenerator { |
publicstaticvoidprocess(Documentdocument, JSONObjectjson) throwsJSONException, DocumentException { |
for (String k : json.keySet()) { |
Object object = json.get(k); |
if (object instanceofJSONArray) { |
JSONArray list = json.getJSONArray(k); |
process(document, list); |
} elseif (object instanceofJSONObject) { |
process(document, json.getJSONObject(k)); |
} else { |
document.add(newParagraph(k +''+ json.get(k))); |
} |
} |
} |
publicstaticvoidprocess(Documentdocument, JSONArrayjson) throwsJSONException, DocumentException { |
for (int x =0; x < json.length(); x++) { |
Object object = json.get(x); |
if (object instanceofJSONArray) { |
JSONArray list = json.getJSONArray(x); |
process(document, list); |
} elseif (object instanceofJSONObject) { |
process(document, json.getJSONObject(x)); |
} else { |
document.add(newParagraph(json.get(x).toString())); |
} |
} |
} |
publicstaticFilejsonTopdf(JSONObjectjson) throwsIOException, DocumentException { |
Document document =newDocument(PageSize.A4, 70, 55, 100, 55); |
File file =File.createTempFile('consulta', '.pdf'); |
FileOutputStream output =newFileOutputStream(file); |
PdfWriter writer =PdfWriter.getInstance(document, output); |
writer.setEncryption('a'.getBytes(), 'b'.getBytes(), PdfWriter.ALLOW_PRINTING, PdfWriter.STANDARD_ENCRYPTION_128); |
writer.createXmpMetadata(); |
writer.setBoxSize('art', newRectangle(36, 54, 559, 788)); |
document.open(); |
document.addCreationDate(); |
document.addTitle('documento'); |
document.newPage(); |
process(document, json); |
document.close(); |
return file; |
} |
} |
commented Jun 7, 2019 • edited
edited
json.keySet() is unresolved anyfix? (Line 24) |
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment
gistfile1
/* |
derived from Getfree's parseJson function |
http://www.autohotkey.com/board/topic/93300-what-format-to-store-settings-in/#entry588268 |
credits to Getfree |
*/ |
JSON_parse(jsonStr) { |
SC := ComObjCreate('ScriptControl') |
SC.Language := 'JScript' |
ComObjError(false) |
jsCode = |
( |
function arrangeForAhkTraversing(obj) { |
if(obj instanceof Array) { |
for(var i=0 ; i<obj.length ; ++i) |
obj[i] = arrangeForAhkTraversing(obj[i]) ; |
return ['array',obj] ; |
} else if(obj instanceof Object) { |
var keys = [], values = [] ; |
for(var key in obj) { |
keys.push(key) ; |
values.push(arrangeForAhkTraversing(obj[key])) ; |
} |
return ['object',[keys,values]] ; |
} else |
return [typeof obj,obj] ; |
} |
) |
SC.ExecuteStatement(jsCode '; obj=' jsonStr) |
return convertJScriptObj2AHK(SC.Eval('arrangeForAhkTraversing(obj)')) |
} |
convertJScriptObj2AHK(jsObj) { |
if (jsObj[0]='object') { |
obj := {}, keys := jsObj[1][0], values := jsObj[1][1] |
loop % keys.length |
obj[keys[A_Index-1]] := convertJScriptObj2AHK(values[A_Index-1]) |
return obj |
} else if (jsObj[0]='array') { |
array := [] |
loop % jsObj[1].length |
array.Insert(convertJScriptObj2AHK(jsObj[1][A_Index-1])) |
return array |
} else return jsObj[1] |
} |
Json File Download
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment