I'm using LitJson in a Visual C# Program that I'm creating.
I'm making a while loop like this...
var JsonData = // Parse some sort of json file
var selectedArray = "exampleArray"
while (!JsonData.Keys.Contains(selectedArray))`
{
// show an input box to reassign the selectedArray string
// other wise if the JSON data Contains a key with a name that matches selectedArray. exit the while loop.
}
now I get this error
System.InvalidOperationException occurred
HResult=0x80131509
Message=Instance of JsonData is not a dictionary
Source=LitJson
StackTrace:
at LitJson.JsonData.EnsureDictionary()
at LitJson.JsonData.get_Keys()
at Parser_V1.ParserMain.ParseJSON() in C:\Users\lenovo8.1\documents\visual studio 2017\Projects\Parser V1\Parser V1\Main.cs:line 107
at Parser_V1.ParserMain.ParseJsonMenuItem_Click_1(Object sender, EventArgs e) in C:\Users\lenovo8.1\documents\visual studio 2017\Projects\Parser V1\Parser V1\Main.cs:line 161
at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
at System.Windows.Forms.ToolStripMenuItem.OnClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.ToolStripDropDown.OnMouseUp(MouseEventArgs mea)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ToolStrip.WndProc(Message& m)
at System.Windows.Forms.ToolStripDropDown.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Parser_V1.Program.Main() in C:\Users\lenovo8.1\documents\visual studio 2017\Projects\Parser V1\Parser V1\Program.cs:line 19
the file looks like this...
[
{
"Name": "Scrap Axe",
"Time": 1,
"CraftingArea": "Diy",
"Output": "Wooden Axe",
"Count": 1,
"0,3": "Wood Plank",
"1,3": "Stick",
"1,4": "Wood Plank",
"2,2": "Stick",
"3,1": "Stick"
},
{
"Name": "Scrap Shovel",
"Time": 20,
"CraftingArea": "Diy",
"Output": "Scrap Shovel",
"Count": 1,
"0,4": "Stick",
"1,3": "Stick",
"2,2": "Stick",
"3,0": "Iron Ore",
"3,1": "Iron Ore",
"4,0": "Iron Ore",
"4,1": "Iron Ore"
},
{
"Name": "Scrap Pickaxe",
"Time": 17,
"CraftingArea": "Diy",
"Output": "Scrap Pickaxe",
"Count": 1,
"0,2": "Iron Ore",
"0,3": "Wood Plank",
"0,4": "Crude Nail",
"1,3": "Stick",
"1,4": "Wood Plank",
"2,2": "Stick",
"2,4": "Iron Ore",
"3,1": "Stick"
},
{
"Name": "Metal Pipe",
"Time": 3,
"CraftingArea": "Diy",
"Output": "Metal Pipe",
"Count": 3,
"1,3": "Iron Ore",
"2,2": "Iron Ore",
"3,1": "Iron Ore"
},
{
"Name": "Water Bottle",
"Time": 15,
"CraftingArea": "CookingStation",
"Output": "Water Bottle",
"Count": 1,
"1,2": "Unpurified Water",
"2,1": "Coal Ore",
"2,2": "Coal Ore",
"2,3": "Coal Ore"
},
{
"Name": "Cooked Alien Meat",
"Time": 30,
"CraftingArea": "CookingStation",
"Output": "Cooked Alien Meat",
"Count": 1,
"1,3": "Raw Alien Meat",
"2,3": "Coal Ore",
"2,2": "Water Bottle"
},
{
"Name": "Coal Chunk",
"Time": 3,
"CraftingArea": "CookingStation",
"Output": "Coal Chunk",
"Count": 2,
"1,1": "Coal Ore",
"1,2": "Coal Ore",
"2,1": "Coal Ore",
"2,2": "Coal Ore"
},
{
"Name": "First Aid Bandage",
"Time": 7,
"CraftingArea": "Diy",
"Output": "First Aid Bandage",
"Count": 1,
"1,2": "Cloth",
"2,2": "Cloth"
},
{
"Name": "Iron Pickaxe",
"Time": 90,
"CraftingArea": "Diy",
"Output": "Iron Pickaxe",
"Count": 1,
"0,2": "Iron Ingot",
"0,3": "Iron Ingot",
"1,3": "Stick",
"1,4": "Iron Ingot",
"2,2": "Stick",
"2,4": "Iron Ingot",
"3,1": "Stick"
},
{
"Name": "Steel Pickaxe",
"Time": 90,
"CraftingArea": "Diy",
"Output": "Steel Pickaxe",
"Count": 1,
"0,2": "Steel Ingot",
"0,3": "Steel Ingot",
"1,3": "Stick",
"1,4": "Steel Ingot",
"2,2": "Stick",
"2,4": "Steel Ingot",
"3,1": "Stick"
},
{
"Name": "Wood Planks From Logs",
"Time": 3,
"CraftingArea": "Diy",
"Output": "Wood Plank",
"Count": 4,
"2,2": "Wood Log"
},
{
"Name": "Wood Planks From Sticks",
"Time": 10,
"CraftingArea": "Diy",
"Output": "Wood Plank",
"Count": 1,
"1,2": "Stick",
"2,3": "Stick",
"2,2": "Crude Nail",
"2,1": "Stick",
"3,2": "Stick"
},
{
"Name": "Sticks From Planks",
"Time": 1,
"CraftingArea": "Diy",
"Output": "Stick",
"Count": 4,
"2,2": "Wood Plank",
"3,1": "Wood Plank"
},
{
"Name": "Sticks From Logs",
"Time": 4,
"CraftingArea": "Diy",
"Output": "Stick",
"Count": 8,
"2,2": "Wood Log"
},
{
"Name": "Iron Shovel",
"Time": 45,
"CraftingArea": "Diy",
"Output": "Iron Shovel Planks",
"Count": 4,
"0,4": "Stick",
"1,3": "Stick",
"2,2": "Stick",
"3,0": "Iron Ingot",
"3,1": "Iron Ingot",
"4,0": "Iron Ingot",
"4,1": "Iron Ingot"
},
{
"Name": "Digging Stick",
"Time": 5,
"CraftingArea": "Diy",
"Output": "Digging Stick",
"Count": 1,
"1,3": "Stick",
"2,2": "Stick"
},
{
"Name": "Small Stone From Stone Petals",
"Time": 1,
"CraftingArea": "Diy",
"Output": "Small Stone",
"Count": 1,
"1,1": "Stone Petals",
"1,2": "Stone Petals",
"2,1": "Stone Petals",
"2,2": "Stone Petals"
},
{
"Name": "Steel Ingot",
"Time": 60,
"CraftingArea": "Forge",
"Output": "Steel Ingot",
"Count": 1,
"1,1": "Coal Chunk",
"1,2": "Coal Chunk",
"1,3": "Coal Chunk",
"2,1": "Coal Chunk",
"2,2": "Iron Ingot",
"2,3": "Coal Chunk",
"3,1": "Coal Chunk",
"3,2": "Coal Chunk",
"3,3": "Coal Chunk"
},
{
"Name": "Steel Shovel",
"Category": "Misc",
"Time": 145,
"CraftingArea": "Diy",
"Output": "Steel Shovel",
"Count": 4,
"0,4": "Stick",
"1,3": "Stick",
"2,2": "Stick",
"3,0": "Steel Ingot",
"3,1": "Steel Ingot",
"4,0": "Steel Ingot",
"4,1": "Steel Ingot"
},
{
"Name": "Copper Wire",
"Time": 1,
"CraftingArea": "Diy",
"Output": "Copper Wire",
"Count": 2,
"1,3": "Copper Ingot",
"2,2": "Copper Ingot",
"3,1": "Copper Ingot"
},
{
"Name": "Metal Plate",
"Time": 4,
"CraftingArea": "Forge",
"Output": "Metal Plate",
"Count": 8,
"2,2": "Iron Ingot"
},
{
"Name": "Dirt Wall",
"Time": 1,
"CraftingArea": "Diy",
"Output": "Dirt Wall",
"Count": 1,
"1,1": "Dirt",
"1,2": "Dirt",
"1,3": "Dirt",
"2,1": "Dirt",
"2,2": "Dirt",
"2,3": "Dirt"
},
{
"Name": "Metal Wall",
"Time": 4,
"CraftingArea": "Diy",
"Output": "Metal Wall",
"Count": 1,
"1,1": "Metal Plate",
"1,2": "Metal Plate",
"1,3": "Metal Plate",
"2,1": "Metal Plate",
"2,2": "Metal Plate",
"2,3": "Metal Plate"
},
{
"Name": "Campfire",
"Time": 6,
"CraftingArea": "Diy",
"Output": "Campfire",
"Count": 1,
"2,1": "Stick",
"1,2": "Stick",
"2,3": "Stick",
"3,1": "Small Stone",
"3,2": "Small Stone",
"3,3": "Small Stone"
},
{
"Name": "Builder Tool",
"Time": 1,
"CraftingArea": "Diy",
"Output": "Builder Tool",
"Count": 1,
"2,1": "Stick",
"1,2": "Stick",
"2,2": "Stick",
"1,3": "Stick"
},
{
"Name": "Clay Forge",
"Time": 15,
"CraftingArea": "Diy",
"Output": "Clay Forge",
"Count": 1,
"0,2": "Clay",
"1,1": "Clay",
"1,2": "Wood Log",
"1,3": "Clay",
"2,1": "Clay",
"2,3": "Clay",
"2,2": "Small Stone"
},
{
"Name": "Fueled Generator",
"Time": 32,
"CraftingArea": "Diy",
"Output": "Fueled Generator",
"2,1": "Metal Plate",
"2,2": "Metal Plate",
"2,3": "Metal Plate",
"3,1": "Metal Plate",
"4,1": "Metal Plate",
"4,2": "Metal Plate",
"4,3": "Metal Plate",
"3,3": "Metal Plate",
"0,1": "Copper Ingot",
"0,2": "Copper Wire",
"0,3": "Copper Ingot",
"1,2": "Copper Ingot"
},
{
"Name": "Crude Nail",
"Time": 0,
"CraftingArea": "Diy",
"Output": "Crude Nail",
"2,2": "Iron Ore"
},
{
"Name": "AI Chip",
"Time": 10,
"CraftingArea": "BuildingBench",
"Output": "AI Chip",
"2,1": "Metal Plate",
"2,2": "Metal Plate",
"1,1": "Copper Wire",
"1,2": "Iron Ingot",
"2,3": "Metal Plate"
},
{
"Name": "Light Machinegun Turret",
"Time": 10,
"CraftingArea": "BuildingBench",
"Output": "Light Machinegun Turret",
"1,1": "AI Chip",
"1,3": "Iron Stick",
"1,4": "Iron Stick",
"1,2": "Metal Plate",
"0,2": "Metal Plate",
"0,1": "Copper Ingot",
"0,0": " Copper Wire",
"2,1": "Iron Stick",
"2,3": "Iron Stick",
"3,1": "Iron Stick",
"3,3": "Iron Stick",
"4,1": "Iron Stick",
"4,3": "Iron Stick"
}
]
Is there's a way I can check if the JSON file is a dictionary? since the file above is an array of objects!
I use LitJSON and work in visual studio 2017 community edition.
I do not know LitJSON at all, so I just read the quickstart, which seems to be all that is available.
There is one thing that needs to be clear, what is a dictionary? From what I understand, there is no such thing as a dictionary json file. A dictionary is part of the file, something like:
"name" : "The Dark Side of the Moon",
So in order to read the file, just deserialize it to an object:
JsonData data = JsonMapper.ToObject(json_text);
And then use:
// Dictionaries are accessed like a hash-table
Console.WriteLine("Album's name: {0}", data["album"]["name"]);
I do not know what happens if album/name does not exist, but I expect that it will result in an exception. As you also got an exception because you were using a dictionary method on an object.
One way to validate the file is to just process it. If an exception occurs then it is invalid. This may not be the best way, but it's a way.
The alternative may be to parse the file as described on the quickstart page using a json reader.
public static void PrintJson(string json)
{
JsonReader reader = new JsonReader(json);
Console.WriteLine ("{0,14} {1,10} {2,16}", "Token", "Value", "Type");
Console.WriteLine (new String ('-', 42));
// The Read() method returns false when there's nothing else to read
while (reader.Read()) {
string type = reader.Value != null ?
reader.Value.GetType().ToString() : "";
Console.WriteLine("{0,14} {1,10} {2,16}",
reader.Token, reader.Value, type);
}
}
In your case you want it to produce something like:
Token Value Type
------------------------------------------
ObjectStart
PropertyName name System.String
String Bill System.String
PropertyName age System.String
Int 32 System.Int32
PropertyName awake System.String
Boolean True System.Boolean
PropertyName n System.String
Double 1994.0226 System.Double
ObjectEnd
It just occured to me that you'll always have at least one object. So you should read 'ObjectStart' once. If it occurs more than once then you know the file has not the expected format. Otherwise you can process the file.
User contributions licensed under CC BY-SA 3.0