Hello.
I need some help with parsing a Json file I'm having trouble with. It's not very structured. Apart from the first four fields, the other's are not always present. Even when they are present, they can vary between records. This is a sample of the file.
{
"word": "elephant",
"lang": "English",
"heads": [{
"1": "~",
"template_name": "en-noun"
}
],
"senses": [{
"glosses": ["A mammal of the order Proboscidea, having a trunk, and two large ivory tusks jutting from the upper jaw."]
}, {
"glosses": ["synonym of elephant paper"],
"tags": ["paper", "printing"],
"taxon": [["Euploea", "genus"]],
"alt_of": ["elephant paper"]
}, {
"glosses": ["A dark grayish-blue colour, like that of an elephant (also called elephant gray)."]
}
],
"synonyms": [{
"word": "word meaning"
}, {
"word": "Elephas maximus",
"sense": "animal"
}, {
"word": "see Appendix:Words used as placeholders to count seconds",
"sense": "counting term"
}
],
"hyponyms": [{
"word": "African forest elephant",
"sense": "animal"
}, {
"word": "Indian elephant",
"sense": "animal"
}
],
"derived": [{
"word": "African bush elephant"
}, {
"word": "white elephant"
}
],
"related": [{
"word": "chryselephantine"
}, {
"word": "Elephas"
}
],
"categories": ["English basic words", "Elephants", "Paper sizes"],
"pos": "noun"
}
I'm up to "senses" bit at the moment. I'm not able to access the array inside the "taxon" array. If the "taxon" field is not present then it works, otherwise understandably it fails. This is what I have so far. Thank you.
procedure Tform1.dojson(jsonstr: string);
var
keyname: string;
jdata: TJSONData;
i, j, x, mainloop: integer;
tempjobj, dataarrayitem: tjsonobject;
en_word, language, Value: string;
sensesarray, headsarray, synonymsarray: TJSONArray;
begin
jdata := GetJSON(jsonstr);
tempjobj := Tjsonobject(jdata);
en_word := tempjobj.FindPath('word').AsString;
language := tempjobj.FindPath('lang').AsString;
for mainloop := 2 to jdata.Count - 1 do
begin
keyname := tjsonobject(jdata).Names[mainloop];
if keyname = 'heads' then
begin
headsarray := TJSONArray(jdata.items[mainloop]);
DataArrayItem := headsarray.Objects[0];
for I := 0 to dataarrayitem.Count - 1 do
begin
keyname := dataarrayitem.Names[i];
Value := DataArrayItem[keyname].AsString;
end;
end
else if keyname = 'senses' then
begin
sensesarray := TJSONArray(jdata.items[mainloop]);
for i := 0 to sensesarray.Count - 1 do
begin
DataArrayItem := sensesarray.Objects[i];
for j := 0 to dataarrayitem.Count - 1 do
begin
keyname := dataarrayitem.Names[j];
Value := dataarrayitem.FindPath(keyname + '[0]').AsString;
end;
end;
end;
end;
jData.Free;
end;