1

Using the BOOMLAGOON library.

I'm trying to generate a JSON object in C# with Unity3d.

string GroupName="Night Fighters";
string GroupMemberID="TG1";
string GroupMemberName="Tommy Gun";
string GroupEquipmentDescription="Tommy Gun";

public JSONObject Testjson()
{        

    var data = new JSONObject
    {
        {"Group Name" , GroupName},
        {"Group Member" , GroupMemberID, GroupMemberName},
        {"Group Equipment" , GroupEquipmentID, GroupEquipmentDescription}
    };    

    Debug.Log(data);

    return data;
}

It's telling me that the overload is wrong - add takes three arguments. Not sure how to do a nested Member or equipment. I've tried () and {} and []. Nothing works.

1
  • Could you post the final result of JSON you are hoping to achieve? Commented Jul 15, 2016 at 0:24

1 Answer 1

1

It is more likely telling that it does not take 3 arguments:

    public void Add(string key, JSONValue value) {
        values[key] = value;
    }

    public void Add(KeyValuePair<string, JSONValue> pair) {
        values[pair.Key] = pair.Value;
    }

But here is what you do:

var data = new JSONObject
{
    {"Group Name" , GroupName},
    {"Group Member" , GroupMemberID, GroupMemberName},  // here three
    {"Group Equipment" , GroupEquipmentID, GroupEquipmentDescription} // Here three
};

You would have to first create a json object where you add both values and then pass that single top object in the ctor.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks Everts! Just what I needed.
Part 2: Got the initial object put together and saved, YEAH! Now, that the initial object exists, what would be the procedure to add another group member and a few more items to the equipment. How then do I save it without overwriting the current file? I've tried appendall with some odd results. (It decided to save old values even after I changed and saved the script.) Any thoughts?
You'd get the equipment json object and add just like you did then add the modified equipment object back into the main object. I am not sure about saving without overwriting, what do u mean there?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.