2017-08-11 78 views
0

我在创建的Visual C#程序中使用LitJson。检查一个json文件是否是字典

我做这样一个while循环...

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. 
} 

现在我得到这个错误

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 

文件看起来像这样...

[ 
    { 
     "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" 
    } 
] 

有没有办法可以检查JSON文件是字典?因为上面的文件是一个对象数组!

我使用LitJSON并在Visual Studio 2017社区版中工作。

+0

任何你使用LitJSON的原因,它在3年内没有被修改过,并且从未达到1.0,而不是像Json.NET这样更常见(也是更维护)的库? –

回答

0

我根本不知道LitJSON,所以我只是读了quickstart,这似乎是所有可用的。

有一点需要清楚,什么是字典?据我所知,没有字典json文件这样的事情。字典是文件的一部分,是这样的:

"name" : "The Dark Side of the Moon", 

所以为了读取文件,只需将其反序列化对象:

JsonData data = JsonMapper.ToObject(json_text); 

然后用:

// Dictionaries are accessed like a hash-table 
Console.WriteLine("Album's name: {0}", data["album"]["name"]); 

我不知道如果专辑/名称不存在会发生什么,但我期望它会导致异常。由于您在对象上使用字典方法,因此您也遇到异常。

验证文件的一种方法是只处理它。如果发生异常,那么它是无效的。这可能不是最好的方式,但它是一种方式。

另一种方法是使用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); 
    } 
} 

你的情况,你希望它生产的东西,如:

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 

它只是发生在我,你会永远拥有至少一个对象。所以你应该阅读'ObjectStart'一次。如果它发生多次,那么你知道该文件没有预期的格式。否则,您可以处理该文件。

相关问题