João Neto João Neto - 4 months ago 18
JSON Question

Make a window with my setting using kivy


  • Values of Control: "ID", "measuredH and measuredW", "typeID", TitleWindow", "x", "y" and "zOrder".

  • Each value of Key Properties.

  • Values of Controls: "measuredH", "measuredW", "mockupH": "mockupW" and "version": "1.0"



Json File - Data/BalsamiqJsonData.json



{
"mockup": {
"controls": {
"control": [
{
"ID": "0",
"h": "390", // Height - Window
"measuredH": "400",
"measuredW": "450",
"properties": {
"bottomheight": "29",
"text": "W4 System", // Title Window
"topheight": "26"
},
"typeID": "TitleWindow", // Know this control is a window.
"w": "438", // Width Window
"x": "181", // Position X Window.
"y": "19", // Position Y Window.
"zOrder": "0"
}
]
},
"measuredH": "409",
"measuredW": "619",
"mockupH": "390",
"mockupW": "438",
"version": "1.0"
}


}

FIle - main.py



import kivy
kivy.require("1.9.1")

from kivy.app import App
from kivy.config import Config
from element import *


path_file = 'Data/BalsamiqJsonData.json'

class WinBallApp(App):

Config.rsizable = 0

def build(self):
Element().open_file(path_file)


if __name__ == "__main__":
WinBallApp().run()


File - element.py



import json
from kivy.config import Config
from kivy.core.window import Window
from kivy.core.window import WindowBase

class Element():

def __init__(self, elem_id = 0, elem_type = "", elem_caption = "",
elem_height = 0, elem_width = 0, elem_pos_x = 0, elem_pos_y = 0,
elem_z_order = 0, elem_text = "", version = 0):

self.elem_id = elem_id
self.elem_type = elem_type
self.elem_caption = elem_caption
self.elem_height = elem_height
self.elem_width = elem_width
self.elem_pos_x = elem_pos_x
self.elem_pos_y = elem_pos_y
self.elem_z_order = elem_z_order
self.elem_text = elem_text

def show_data(self, *args):
print(args)


def make_window(self):


Window.set_title(self.elem_caption)
Window.size = (self.elem_height, self.elem_width)


def check_type(self, elem_type):

if elem_type == 'TitleWindow':
self.make_window()


def manipulate_data(self, data):

for mockup_key, mockup_value in data.items():

mockup_control = mockup_value['controls']['control']

self.elem_id = int(mockup_control[0]['ID'])
self.elem_type = mockup_control[0]['typeID']
self.elem_caption = mockup_control[0]['properties']['text']
self.elem_height = int(mockup_control[0]['h'])
self.elem_width = int(mockup_control[0]['w'])
self.elem_pos_x = int(mockup_control[0]['x'])
self.elem_pos_y = int(mockup_control[0]['y'])
self.elem_z_order = int(mockup_control[0]['zOrder'])

self.show_data(self.elem_id, self.elem_type, self.elem_caption, self.elem_height,
self.elem_width, self.elem_pos_x, self.elem_pos_y, self.elem_z_order)

self.check_type(self.elem_type)


def open_file(self, path_file):
with open(path_file, mode='r') as jsonData:
load_data = json.load(jsonData)

self.manipulate_data(load_data)



Ask - How I change the settings window using the values of json file like title, position x, position y, height, width ?

Answer

Using org.simple.json

You may have notice that each get method returns an Object. That is the reason the library has the word "simple." It is a very minimal library and you'll need to cast each object independently. Therefore chaining operations cannot be done cleanly.

JSONParser parser = new JSONParser();

try {
    JSONObject obj = (JSONObject) parser.parse(new FileReader("data.json"));

    JSONObject mockup = (JSONObject) obj.get("mockup");
    JSONObject controls = (JSONObject) mockup.get("controls");
    JSONArray control = (JSONArray) controls.get("control");

    for (int i = 0; i < control.size(); i++) {
        JSONObject controlObj = (JSONObject) control.get(i);
        System.out.println(String.valueOf(controlObj.get("typeID")));
    }

} catch (ParseException e) {
    e.printStackTrace();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}