JSON-P Leer arhivos
Proyecto
Clonar
git clone https://avbravo@bitbucket.org/avbravo/fiscalencripter.git
En el archivo index.xhtml
<p:fieldset legend="LEER ARCHIVO JSON" style="margin-bottom:20px" toggleable="true" collapsed="true">
<p:panelGrid columns="3">
<p:commandButton value="Read json"
update=":form, :form:datatable"
action="#{jsonpController.readJson()}"/>
</p:panelGrid>
<p:dataTable value="#{jsonpController.licenseList}"
id="datatable"
var="item">
<p:column headerText="idlicense">
<p:outputLabel value="#{item.idlicense}"/>
</p:column>
<p:column headerText="key">
<p:outputLabel value="#{item.key}"/>
</p:column>
<p:column headerText="company">
<p:outputLabel value="#{item.company}"/>
</p:column>
<p:column headerText="description">
<p:outputLabel value="#{item.description}"/>
</p:column>
<p:column headerText="system">
<p:outputLabel value="#{item.system}"/>
</p:column>
<p:column headerText="title">
<p:outputLabel value="#{item.title}"/>
</p:column>
</p:dataTable>
</p:fieldset>
Crear la clase JsonpController.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.javscaz.fiscalweb.fiscalencripter;
import com.avbravo.jmoordbutils.JsfUtil;
import com.javscaz.fiscalweb.fiscalencripter.entity.License;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.Serializable;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Named;
import javax.faces.view.ViewScoped;
import javax.json.Json;
import javax.json.stream.JsonParser;
import javax.json.stream.JsonParserFactory;
/**
*
* @author avbravo
*/
@Named(value = "jsonpController")
@ViewScoped
public class JsonpController implements Serializable {
private static final long serialVersionUID = 1L;
List<License> licenseList = new ArrayList<>();
public List<License> getLicenseList() {
return licenseList;
}
public void setLicenseList(List<License> licenseList) {
this.licenseList = licenseList;
}
/**
* Creates a new instance of jsonpController
*/
public JsonpController() {
}
// <editor-fold defaultstate="collapsed" desc="String readJson()">
public String readJson() {
try {
InputStream is = new FileInputStream("/home/avbravo/Descargas/license.json");
JsonParserFactory factory = Json.createParserFactory(null);
JsonParser parser = factory.createParser(is, StandardCharsets.UTF_8);
if (!parser.hasNext() && parser.next() != JsonParser.Event.START_ARRAY) {
JsfUtil.warningDialog("warning", "No se abrio el archivo");
return "";
}
// looping over object attributes
licenseList = new ArrayList<>();
License license = new License();
while (parser.hasNext()) {
System.out.println("<----------------------------------->");
System.out.println("<----------------------------------->");
JsonParser.Event event = parser.next();
// starting object
if (event == JsonParser.Event.START_OBJECT) {
while (parser.hasNext()) {
event = parser.next();
if (event == JsonParser.Event.KEY_NAME) {
String key = parser.getString();
switch (key) {
case "idlicense":
parser.next();
license = new License();
license.setIdlicense(parser.getString());
System.out.printf("id: %s%n", parser.getString());
break;
case "key":
parser.next();
license.setKey(parser.getString());
System.out.printf("key: %s%n", parser.getString());
break;
case "system":
parser.next();
license.setSystem(parser.getString());
System.out.printf("title: %s%n", parser.getString());
break;
case "title":
parser.next();
license.setTitle(parser.getString());
System.out.printf("title: %s%n", parser.getString());
break;
case "description":
parser.next();
license.setDescription(parser.getString());
System.out.printf("description: %s%n%n", parser.getString());
break;
case "company":
parser.next();
license.setCompany(parser.getString());
System.out.printf("company: %s%n%n", parser.getString());
break;
case "author":
parser.next();
license.setAuthor(parser.getString());
System.out.printf("Author: %s%n%n", parser.getString());
System.out.println("----> agregando al lit");
licenseList.add(license);
break;
}
}
}
}
}
JsfUtil.infoDialog("Terminado", "Proceso terminado...size(): "+licenseList.size());
System.out.println("IMPRIMIR LAS LICENCIAS");
for(License l:licenseList){
System.out.println("---> idlicense "+l.getIdlicense());
System.out.println("---> KEY "+l.getKey());
}
return "";
} catch (Exception e) {
JsfUtil.errorDialog("error ", e.getLocalizedMessage());
}
return "";
}
// </editor-fold>
}
Salida
Archivo license.json
[
{
"idlicense":"1",
"system": "fiscalprinter",
"key": "?????????????",
"title": "Impresora Prueba",
"company": "Grupo Moreno",
"description": "Grupo moreno",
"author": "Javscaz"
},
{
"idlicense":"2",
"system": "fiscalprinter",
"key": "TDDF2LLS",
"title": "Impresora lOS sANTOS",
"company": "Grupo Moreno",
"description": "Grupo moreno",
"author": "Javscaz"
}
]
Entity
public class License {
private String idlicense;
private String system;
private String key;
private String title;
private String company;
private String description;
private String author;
public License() {
}
public String getIdlicense() {
return idlicense;
}
public void setIdlicense(String idlicense) {
this.idlicense = idlicense;
}
public String getSystem() {
return system;
}
public void setSystem(String system) {
this.system = system;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getCompany() {
return company;
}
public void setCompany(String company) {
this.company = company;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
}
Last updated