Konvertierung von json zum Java Objekt nach vorgegebenem Schema

q-jack

Mitglied
Hallo Zusammen,
Ich hoffe ihr könnt mir helfen. Google konnte mir mit meinem Problem nicht helfen.
Ich habe folgendes Schema:

// schema of the 101 wiki in the Entity Relationship Schema Notation
// (see megalib/ERGraphGraph.php)
// @ means key
// ! means 1
// ? means 0..1
// * means *
// This file is used in particular in the context of the module Wiki101JsonToWiki101Rdf
// to drive the transformation from Json to RDF (via ERGraph).
//
// IMPORTANT: Only the classes and attributes listed below will be converted
// This means that there could be more classes and attributes in the original json file.
// Attributes names should be the same as those in the json files.
// There is a mapping file for the binding between class and top level category.


// Note (1): Some fields are currently ignored because the fact that they contain XML
// leads to problems in graphml generation. Will be fixed later.

Category {
id: string@ ;
name: string! ;
type: string! ;
url: string! ;
headline: string? ;
headline_links: Entity* ;
// discussion:string? ; // See Note (1)
discussion_links:Entity* ;
categories: Category* ;
languageMembers: Language* ;
conceptMembers: Concept* ;
featureMembers: Feature* ;
implementationMembers: Implementation* ;
technologyMembers: Technology*
}

Concept {
id: string@ ;
name: string! ;
type: string! ;
url: string! ;
headline:string? ;
headline_links: Entity* ;
// discussion:string? ; // See Note (1)
discussion_links:Entity* ;
}

Feature {
id: string@ ;
name: string! ;
type: string! ;
url: string! ;
headline:string! ;
headline_links: Entity* ;
// description:string? ; // See Note (1)
description_links:Entity* ;
// illustration:string? ; // See Note (1)
illustration_links:Entity* ;
implementations:Implementation*
}

Implementation {
id: string@ ;
name: string! ;
type: string! ;
url: string! ;
headline: string! ;
headline_links: Entity* ;
// motivation: string! ; // See Note (1)
motivation_links:Entity* ;
features: Feature* ;
languages: Language* ;
technologies: Technology* ;
// usage:string? ; // See Note (1)
usage_links:Entity* ;
}

Language {
id: string@ ;
name: string! ;
type: string! ;
url: string! ;
headline: string! ;
headline_links: Entity* ;
// description: string? ; // See Note (1)
description_links:Entity* ;
implementations: Implementation*
}

Technology {
id: string@ ;
name: string! ;
type: string! ;
url: string! ;
headline: string! ;
headline_links: Entity* ;
// description: string? ; // See Note (1)
description_links:Entity* ;
implementations: Implementation*
}

Page {
id: string@ ;
name: string! ;
type: string! ;
url: string! ;
headline: string! ;
headline_links: Entity* ;
// discussion:string? ; // See Note (1)
discussion_links:Entity*
}

und eine json datei. ich poste nur einen einen kleinen teil daraus, da diese sehr lang ist.

{

"Category": {
"AspectADASHoriented_programming_language": {
"languageMembers": [
{
"type": "Language",
"name": "AspectJ"
}
],
"discussion_links": [ ],
"name": "Aspect-oriented programming language",
"categoryMembers": [ ],
"pageMembers": [ ],
"url": "http://101companies.org/index.php/Category:Aspect-oriented programming language",
"headline_links": [
{
"type": "Concept",
"name": "aspectADASHoriented_programming_paradigm"
}
],
"discussion": "",
"technologyMembers": [ ],
"headline": "a programming language in the [[aspect-oriented programming paradigm]]",
"conceptMembers": [ ],
"implementationMembers": [ ],
"type": "Category",
"categories": [ ],
"featureMembers": [ ]
},
"XML_technology": {

Ich habe in java die einzelnen Klassen aus dem Schema mit gettern und settern erstellt, sowie ein toString methode implementiert.
in der main lese ich das file ein, aber bekomme bei der Ausgabe nur null-Werte.
Ich habe etwas in der Richtung gelesen, dass ich eine zusätzliche Klasse brauche, die HasMaps der einzelnen Klassen enthalten soll, aber ich kann damit nichts anfangen bzw. verstehe den Sinn nicht.
Hier mein Code aus der main, über tipps wäre ich sehr dankbar

Java:
public class JsonQueryApi {

	public static void main(String [] args) throws IOException{

	try {
		 
		BufferedReader br = new BufferedReader(
			new FileReader("Wiki101Full.json"));
			
		Gson gson = new Gson();
 
		//convert the json string back to object
		Implementation implementation = gson.fromJson(br, Implementation.class);
 
		System.out.println(implementation);
 
	} catch (IOException e) {
		e.printStackTrace();
	}
}
}
und hier einmal die Implementation Klasse
Java:
public class Implementation {
	public String name;
	public String id;
	public String type;
	public String url;
	public String headline;
	public ArrayList<Language> languages;
	public ArrayList<Technology> technologies;
	
	public  Implementation(String name, String id, String type,
			String url, String headline, ArrayList<Language> Languages,ArrayList<Technology>technologies ){
		this.name = name;
		this.id = id;
		this.type  = type;
		this.url=url;
		this.headline=headline;
		
		
	}

	public String getName(){
		return name;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getHeadline() {
		return headline;
	}

	public void setHeadline(String headline) {
		this.headline = headline;
	}

	public ArrayList<Language> getLanguages() {
		return languages;
	}

	public void setLanguages(ArrayList<Language> languages) {
		this.languages = languages;
	}

	public ArrayList<Technology> getTechnologies() {
		return technologies;
	}

	public void setTechnologies(ArrayList<Technology> technologies) {
		this.technologies = technologies;
	}

	public void setName(String name) {
		this.name = name;
	}
	@Override
	public String toString() {
	   return "Implementation [name=" + name + ", id=" + id + ", type="
		+ type +", url=" + url + ", headline=" + headline +"]";
	}

}
 
Du benutzt den library von google, ne?
Mit simple-json.jar könnte ich dir weiterhelfen. Wenn du willst benutze den.
Einfach in google eintippen und downloaden.
Funktioniert im Prinzip gleich, wenn ich mir so deinen Code ansehe.
 
ja. man hat uns empfohlen google zu benutzen aber ich bin gerne bereit was anderes auszuprobieren. Ich lade mir mal die simple-json.jar herunter und probier mal herum.
Über ein kleine Hilfestellung wäre ich dankbar, wenn du sagst dass die fast gleich funktionieren, würde ich ja an der selben Stelle hängen bleiben.
 
Java:
InputStream ijson = new FileInputStream("Wiki101Full.json");
String jsonTxt = IOUtils.toString( ijson );

		   
		   JSONParser parser = new JSONParser();
		   Object obj=parser.parse(jsonTxt);
		   System.out.println(obj);

Das habe ich jetzt eingefügt und der zeigt mir auch schön die Inhalte an, aber ich habe da ja noch keine Struktur drin. Wie bekomme ich das hin? Die Klassen Category, Concept, Implemenatation...siehe Schema stehen ja ganz außen und da drin verschachtelt sind weitere Werte die ich abfragen muss.
Wie kann ich das am Besten lösen. Hatte das mit einer HashMap probiert, aber da hatte ich nicht die Möglichkeit einzelne Werte abzufragen wie z.B. "gib mir alle id von Implementation aus die die Language name=haskell haben
 

Neue Themen


Zurück
Oben