2017-08-11 13 views
0

ある場合。私はこのようなwhileループを作ってるんだチェックJSONファイルは、私が作成していますのVisual C#プログラムでLitJsonを使用している辞書

は...

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

3年間で修正されていないLitJSONを使用していて、Json.NETなど、より一般的な(より管理された)ライブラリではなく、1.0に達していない理由は何ですか? –

答えて

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"]); 

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」一度お読みください。 2回以上出現する場合は、ファイルが予期しない形式であることがわかります。それ以外の場合は、ファイルを処理できます。

関連する問題