- Home /
No viable solution found
Combining 2 lists in particular way...
I have two lists: one is the player names and the other is their equipment. John, Sally, Frank, Ed Rifle, m16, hunting knife, revolver What I want to do is have:
John(new line) m16
Sally(new line) Hunting Knife
Frank(new line) Revolver
Ed(new line) Rifle
Will the JSON Utility accomplish this or do I need something else? Would I use Add range with nested for statements? Or convert one or both to arrays. Am I even in the ballpark?
Thanks!
I'ma little confused. A new line is just some way to format text and JSON is a format for serialization. One can work forgot the other.
What I want to do is have
To have how ? : Displayed on the screen as text? Saved on the disk as pairs of strings? As a json string to be passed to a server or saved to playerprefs?
Answer by Raimi · Jun 12, 2017 at 07:54 PM
You can add stuff to a JSON string like...
{name, level, stenght, weapon, other}
Then store each JSON string in a list.
Then you would get item from list and parse JSON string to retrieve data.
I suggest you use SimpleJSON, look it up, it is well documented;
Answer by aguskos · Jun 12, 2017 at 08:23 PM
It's hard to know what you want but if you want emulate Zip MSDN which is availeble only since .Net 4.0. You can create your extension method.
public static class Helper
{
public static IEnumerable<TResult> Zip<TA, TB, TResult>(
this IEnumerable<TA> seqA, IEnumerable<TB> seqB, Func<TA, TB, TResult> func)
{
if (seqA == null) throw new ArgumentNullException("seqA");
if (seqB == null) throw new ArgumentNullException("seqB");
using (var iteratorA = seqA.GetEnumerator())
using (var iteratorB = seqB.GetEnumerator())
{
while (iteratorA.MoveNext() && iteratorB.MoveNext())
{
yield return func(iteratorA.Current, iteratorB.Current);
}
}
}
}
Then you can do this
List<string> names = new List<string> {"kirill", "mifody", "ktulho"};
List<string> guns = new List<string> { "m1","ak45","non"};
var all = names.Zip(guns,(f,s) => f+" "+s);
foreach (var merged in all)
{
print(merged);
}
output is:
"kirill m1"
"mifody ak45"
...