2017-02-10 77 views
1

所以我試圖採用.json文件並在c#中讀取它,然後修改其中一個值,然後刷新.json文件以便更改該值。我知道如何讀取文件以及如何覆蓋文件,但是如何更改文件中某個特定項目的某個特定值?如何在Unity3D中使用LitJson編輯C#中的JSON值?

此腳本的目標是創建一個庫存系統,其餘額可以通過json文件進行記錄。我想使用BalanceChange()函數轉到文件,使用ID變量找到正確的項目,然後相應地更改餘額。

這是我上傳.json腳本:

[ 
    { 
    "name":"Potion", 
    "id":1, 
    "balance":5 
    }, 
    { 
    "name":"Neutral Bomb", 
    "id":2, 
    "balance": 4 
    } 
] 

這裏是我的C#腳本:

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 
using System.IO; 
using LitJson; 


public class Item 
{ 
    public string Name { get; set; } 
    public int Id  { get; set; } 
    public int Balance { get; set; } 

    public Item(string name1, int id1, int balance1) { 
     Name = name1; 
     Id = id1; 
     Balance = balance1; 
    } 
} 

public class InventoryAttempt : MonoBehaviour 
{ 
    public static void BalanceChange(string filePath, int ID, int change) 
    { 
     string jsonString = File.ReadAllText(Application.dataPath + filePath); 
     List<Item> itemList = JsonMapper.ToObject<List<Item>>(jsonString); 
     itemList [ID].Balance += change; 
     jsonString = JsonMapper.ToJson (itemList).ToString(); 
     File.WriteAllText (Application.dataPath + filePath, jsonString); 
    } 

    void Start() 
    { 
     BalanceChange ("/Scripts/inventory.json", 1, -1); 
    } 
} 

這個腳本是不是出於某種原因的工作。我一直得到這個錯誤:

MissingMethodException: Method not found: 'Default constructor not found...ctor() of Item'. 
System.Activator.CreateInstance (System.Type type, Boolean nonPublic) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Activator.cs:368) 
System.Activator.CreateInstance (System.Type type) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System/Activator.cs:254) 
LitJson.JsonMapper.ReadValue (System.Type inst_type, LitJson.JsonReader reader) 
LitJson.JsonMapper.ReadValue (System.Type inst_type, LitJson.JsonReader reader) 
LitJson.JsonMapper.ToObject[List`1] (System.String json) 
InventoryAttempt.BalanceChange (System.String filePath, Int32 ID, Int32 change) (at Assets/Scripts/InventoryAttempt.cs:26) 
InventoryAttempt.Start() (at Assets/Scripts/InventoryAttempt.cs:34) 
+2

請發佈您正在談論的代碼。 –

+0

考慮閱讀http://stackoverflow.com/help/how-to-ask以獲得您可以獲得的最佳幫助!如果你想得到更具體的幫助,請延長你的問題,就像@DourHighArch說的那樣! – Maakep

回答

0

是的,這就是這些圖書館的目的。我對LitJson並不熟悉,但其中大部分類似:

基於doucmentation示例;我們採取JSON和它轉化爲一個對象,更改屬性,然後將其保存到再次JSON:

public class Person 
{ 
    // C# 3.0 auto-implemented properties 
    public string Name  { get; set; } 
    public int  Age  { get; set; } 
    public DateTime Birthday { get; set; } 
} 


public class Example{ 
    public static void JsonToPerson() 
    { 
    string json = @" 
     { 
      ""Name""  : ""Thomas More"", 
      ""Age""  : 57, 
      ""Birthday"" : ""02/07/1478 00:00:00"" 
     }"; 

    Person thomas = JsonMapper.ToObject<Person>(json); 

    thomas.Age = 23; 

    string newJson = JsonMapper.ToJson(thomas); 
    } 
} 

編輯,我們只需要改變對象屬性的屬性。在我的例子中,我編輯thomas.Age到23,然後再次將對象序列化爲json。 應該給我們以下JSON:

{ 
    ""Name""  : ""Thomas More"", 
    ""Age""  : 23, 
    ""Birthday"" : ""02/07/1478 00:00:00"" 
} 

如果你想有多少人在一個JSON,將它們添加到列表和序列名單。如果您希望爲每個不同的對象提供一個唯一標識符,則可以向Person添加Id屬性。就像這樣:

首先將ID添加屬性格式Person類:

public string Id { get; set; } 

和使用/列表系列化:

List<Person> people = List<Person>(); 

Person thomas = new Person(); 
thomas.Name = "Thomas"; 
thomas.Id = "0001"; 
people.Add(thomas); 

Person albert = new Person(); 
albert.Name = "Albert"; 
albert.Id = "0002"; 
people.Add(albert); 

JsonMapper.ToJson(people); 

這應該給你的Json是這樣的:

{ 
    { 
     ""Name""  : ""Thomas"", 
     ""Id""  : ""0001"" 
    }, 
    { 
     ""Name""  : ""Albert"", 
     ""Id""  : ""0002"" 
    } 
} 
+0

因此,如果我需要多個相同類型的對象,那麼在我的腳本中該如何工作?我需要編制清單,因此我需要訪問多個對象中的一個。 –

+0

您使用這些對象製作一個列表。列表 objList =新列表(); – Maakep

+0

但是,我怎樣才能讓它在列表的每個元素中只有一個對象?基本上我如何分離元素。 –

相關問題