0

Why does this work...

int[] mySadIntArray = new int[] { 2, 3, 4, 8, 9 };
    JSONArray mySadJSONArray = new JSONArray();
    foreach (int i in mySadIntArray)
        mySadJSONArray.Add(i);

// Add Functions

public override void Add(JSONNode aItem)
{
    var tmp = new JSONArray();
    tmp.Add(aItem);
    Set(tmp);
}

public override void Add(string aKey, JSONNode aItem)
{
    var tmp = new JSONObject();
    tmp.Add(aKey, aItem);
    Set(tmp);
}

But not this?

playerJSON.Add("C", mySadIntArray.ToJSONArray());

With...

public static JSONArray ToJSONArray<T>(this IEnumerable<T> array) where T : JSONNode
{
     JSONArray JSONArray = new JSONArray();
     foreach (T node in array)
         JSONArray.Add(node);

     return JSONArray;
}

Gives me the error:

The type 'int' cannot be used as type parameter 'T' in the generic type or method 'ExtensionMethods.ToJSONArray(IEnumerable)'. There is no boxing conversion from 'int' to 'SimpleJSON.JSONNode'.

If there is not boxing conversion, shouldn't it also break in the "Why does this work..." code?

aberdeen
  • 33
  • 4
  • There is likely an overload of the `Add` function that takes `int` as parameter. That's why it works. Your code is not complete so we don't know what that class you are overriding its `Add` functions looks like. – Programmer Aug 31 '17 at 01:09
  • By the way, you should using Unity's [JsonUtility](https://stackoverflow.com/questions/36239705/serialize-and-deserialize-json-and-json-array-in-unity/36244111#36244111) unless there is a good reason not to. – Programmer Aug 31 '17 at 01:09
  • I want to use JsonUtility, but I need to be able to look up individual things and not create objects out of them. How would I generate { "name" : "Bob" "id" : 5 } without having to use an intermediary class object such as class Person { public string name; public int id; }? – aberdeen Aug 31 '17 at 01:49
  • And when I want to retrieve data, I want it to be the same way. 'string name = STRING_OF_JSON["name"].Value;' – aberdeen Aug 31 '17 at 01:51
  • And also, there are no overloads other than these two – aberdeen Aug 31 '17 at 01:54
  • Your usage is one of those times you shouldn't use JsonUtility. For your second code that doesn't work, I don't find ToJSONArray in the `SimpleJSON` class. Also, I suggest you update and show the type of `playerJSON` in your example. – Programmer Aug 31 '17 at 02:54

0 Answers0