How to combine two list items and form JSON data in Unity?

unity list concat
merge two json objects c#
.net merge two lists
merge two json arrays c#
c# combine two lists
c# combine two lists of same type
merge two json strings c#
vb net combine two lists

I was looking to convert items from two list to form JSON data.When I validated the JSON it is showing error.Code given below

public struct ListContainer
{

    public List<PlayerHandler> SaveValues;
    public List<PlayerMovement> NoteValues;


    public ListContainer(List<PlayerHandler> _saveVal,List<PlayerMovement> _noteVal)
    {
        SaveValues = _saveVal;
        NoteValues = _noteVal;

    }

}

//--Adding Two list into the container
ListContainer container = new ListContainer(getAlldata,playerNotes);

    //--Adding data in container into List<string> jsonstring
    jsonstring.Add(JsonUtility.ToJson(container));

Later I am saving the above the List of(CustomClass) jsonstring into a JSON file.The coding for saving it into the persistent path is given below.

public void Save()
{


    //--Get Text typed in the input box
    savedName = saveName.text;

    //--Combing list of string into a single string
    string jsons = string.Join(",", jsonstring);

    //Writing into a JSON file in the persistent path
    using (FileStream fs = new FileStream(Application.persistentDataPath + "/" + savedName+".json" , FileMode.Create))
    {
        BinaryWriter filewriter = new BinaryWriter(fs);

        filewriter.Write(jsons);
        fs.Close();

    }

    saveButtonShow.SetActive(false);

}

When I check the json file from the path and try to validate it is showing error.JSON given below.

 {"SaveValues":[{"id":1,"allposition":{"x":-2.383237361907959,"y":-5.711871147155762},
"allrotation":{"x":0.0,"y":0.0,"z":0.0,"w":1.0},
"allscale":{"x":1.0,"y":1.0},"linepos0":{"x":0.0,"y":0.0,"z":0.0},
"linepos1":{"x":0.0,"y":0.0,"z":0.0},
"movetype":1},{"id":2,"allposition":{"x":-4.0806732177734379,"y":5.998472213745117},
"allrotation":{"x":0.0,"y":0.0,"z":0.0,"w":1.0},"allscale":{"x":1.0,"y":1.0},"linepos0":{"x":0.0,"y":0.0,"z":0.0},
"linepos1":{"x":0.0,"y":0.0,"z":0.0},"movetype":1}],
"NoteValues":[{"movenumber":1,"notemsg":"First Move"}]},{"SaveValues":[{"id":1,"allposition":{"x":-2.383237361907959,"y":-5.711871147155762},
"allrotation":{"x":0.0,"y":0.0,"z":0.0,"w":1.0},
"allscale":{"x":1.0,"y":1.0},"linepos0":{"x":0.0,"y":0.0,"z":0.0},
"linepos1":{"x":0.0,"y":0.0,"z":0.0},
"movetype":2},{"id":1,"allposition":{"x":4.558084964752197,"y":-5.517238140106201},
"allrotation":{"x":0.0,"y":0.0,"z":0.0,"w":1.0},
"allscale":{"x":1.0,"y":1.0},"linepos0":{"x":0.0,"y":0.0,"z":0.0},"linepos1":{"x":0.0,"y":0.0,"z":0.0},"movetype":2},
{"id":2,"allposition":{"x":-4.0806732177734379,"y":5.998472213745117},
"allrotation":{"x":0.0,"y":0.0,"z":0.0,"w":1.0},
"allscale":{"x":1.0,"y":1.0},"linepos0":{"x":0.0,"y":0.0,"z":0.0},"linepos1":{"x":0.0,"y":0.0,"z":0.0},"movetype":2},
{"id":2,"allposition":{"x":4.3838324546813969,"y":4.650305271148682},
"allrotation":{"x":0.0,"y":0.0,"z":0.0,"w":1.0},
"allscale":{"x":1.0,"y":1.0},"linepos0":{"x":0.0,"y":0.0,"z":0.0},"linepos1":{"x":0.0,"y":0.0,"z":0.0},"movetype":2}],
"NoteValues":[{"movenumber":2,"notemsg":"Second Move"}]}

The error it is showing is

Error: Parse error on line 61:
...: "First Move"   }]}, {  "SaveValues": [
---------------------^
Expecting 'EOF', got ','

The JSON validator you're using is expecting a single valid json value, but your file contains two json objects that are separated by a comma.

Consider putting your objects in an array by surrounding the collection of objects with square brackets.

Also, as derHugo noted in the comments, "instead of Application.persistentDataPath + "/" + savedName+".json" you should rather always go for Path.Combine(Application.persistentDataPath, savedName+".json"):

public void Save()
{


    //--Get Text typed in the input box
    savedName = saveName.text;

    //--Combing list of string into a single string
    string jsons = "[" + string.Join(",", jsonstring) + "]";

    //Writing into a JSON file in the persistent path
    using (FileStream fs = new FileStream(
            Path.Combine(Application.persistentDataPath, savedName+".json"), 
            FileMode.Create))
    {
        BinaryWriter filewriter = new BinaryWriter(fs);

        filewriter.Write(jsons);
        fs.Close();

    }

    saveButtonShow.SetActive(false);

}

Which would produce something like this:

[{"SaveValues":[{"id":1,"allposition":{"x":-2.383237361907959,"y":-5.711871147155762},
"allrotation":{"x":0.0,"y":0.0,"z":0.0,"w":1.0},
"allscale":{"x":1.0,"y":1.0},"linepos0":{"x":0.0,"y":0.0,"z":0.0},
"linepos1":{"x":0.0,"y":0.0,"z":0.0},
"movetype":1},{"id":2,"allposition":{"x":-4.0806732177734379,"y":5.998472213745117},
"allrotation":{"x":0.0,"y":0.0,"z":0.0,"w":1.0},"allscale":{"x":1.0,"y":1.0},"linepos0":{"x":0.0,"y":0.0,"z":0.0},
"linepos1":{"x":0.0,"y":0.0,"z":0.0},"movetype":1}],
"NoteValues":[{"movenumber":1,"notemsg":"First Move"}]},{"SaveValues":[{"id":1,"allposition":{"x":-2.383237361907959,"y":-5.711871147155762},
"allrotation":{"x":0.0,"y":0.0,"z":0.0,"w":1.0},
"allscale":{"x":1.0,"y":1.0},"linepos0":{"x":0.0,"y":0.0,"z":0.0},
"linepos1":{"x":0.0,"y":0.0,"z":0.0},
"movetype":2},{"id":1,"allposition":{"x":4.558084964752197,"y":-5.517238140106201},
"allrotation":{"x":0.0,"y":0.0,"z":0.0,"w":1.0},
"allscale":{"x":1.0,"y":1.0},"linepos0":{"x":0.0,"y":0.0,"z":0.0},"linepos1":{"x":0.0,"y":0.0,"z":0.0},"movetype":2},
{"id":2,"allposition":{"x":-4.0806732177734379,"y":5.998472213745117},
"allrotation":{"x":0.0,"y":0.0,"z":0.0,"w":1.0},
"allscale":{"x":1.0,"y":1.0},"linepos0":{"x":0.0,"y":0.0,"z":0.0},"linepos1":{"x":0.0,"y":0.0,"z":0.0},"movetype":2},
{"id":2,"allposition":{"x":4.3838324546813969,"y":4.650305271148682},
"allrotation":{"x":0.0,"y":0.0,"z":0.0,"w":1.0},
"allscale":{"x":1.0,"y":1.0},"linepos0":{"x":0.0,"y":0.0,"z":0.0},"linepos1":{"x":0.0,"y":0.0,"z":0.0},"movetype":2}],
"NoteValues":[{"movenumber":2,"notemsg":"Second Move"}]}]

How to combine two list items and form JSON data in Unity?, The JSON validator you're using is expecting a single valid json value, but your file contains two json objects that are separated by a comma. Note that instead of Application.persistentDataPath + "/" + savedName+".json" you should rather always go for Path.Combine(Application.persistentDataPath, savedName+".json") – derHugo Oct 17 '19 at 6:49

Even if you resolve object bad string exceptions, you have to put them in array, because they have same values and you are doing duplication of keys.

List<object> objList = new List<object>();
        objList.Add(list1);
        objList.Add(list2);

I create code sample for that

https://dotnetfiddle.net/KpZSiF

Combining 2 lists in particular way, 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;. I am developing a Unity game which includes a multiple-choice quiz. I've placed what I believe is a properly-formatted .json file (the quiz question, four answer choices, the correct answer, and an explanation for the answer) (which I checked with a .json formatter) in the StreamingAssets folder in the Unity project.

You should store the containers in a list of containers, not jsons, and serialize just before you write to disk.

var containers = new List<ListContainer>();

(...)

ListContainer container = new ListContainer(getAlldata,playerNotes);
containers.Add(container);

(...)

string json = JsonUtility.ToJson(containers);
// maybe even = JsonUtility.ToJson( new { Players = containers } ); but that's not important.

// Save to file, your way or 
System.IO.File.WriteAllText(path, json);

Combining Lists, I have two lists created from classes which contain two string variables each. This third list is going to be sent into a JSON string and saved in a database. The data types are simply different and incompatible. was adding multiple items of the same type into a list with separate parameters, you problem  This sample merges LINQ to JSON objects. JObject o1 = JObject.Parse(@"{ 'FirstName': 'John', 'LastName': 'Smith', 'Enabled': false, 'Roles': [ 'User' ] }"); JObject

Merging JSON, This sample merges LINQ to JSON objects. Sample. Usage. Copy. JObject o1 = JObject.Parse(@"{ 'FirstName': 'John', 'LastName': 'Smith', 'Enabled': false,  The following algorithm encodes form data as application/json. It operates on the form data set obtained from constructing the form data set. Let resulting object be a new Object. For each entry in the form data set, perform these substeps: If the entry's type is file, set the is file flag.

how to concatenate two list in Code Example, Get code examples like "how to concatenate two list in" instantly right from your google Note: This won't work with single items not in a list. Hi, I want to pull only attachments from Sharepoint list, below is my Flow, i am not sure how to combine all Json results and union them, i want to return the all Json results from all sharepoint list attachments to powerapps. I want display name and aboluteuri properties from list attachments, pl

Unity json array, Add the Firebase Unity SDK (specifically, FirebaseDatabase.unitypackage ) to your ToJson() to convert the object to raw Json and call SetRawJsonValueAsync() . Use the Push() method to append data to a list in multiuser applications. The unique key generated by Push() is based on a timestamp, so list items are  This example shows the use of the "path" constructor option to extract out the data items. This is nothing different from some of the previous examples, but we will build on this in the next example. An abbreviated version of the JSON data is included here for reference. You can see the full JSON data used by this example here.

Comments
  • That's because the JSON ends there ... What you have is a List<ListContainer> or ListContainer[] there ... so it should be sorrounded by [ ... ]
  • But still I cant get values out of it....I tried Debug.Log("Save value count = " + JNode[0]["NoteValues"]["notemsg"]); to get the 'First Move' output
  • well did you try JNode[1]? Also it depends on the library I guess ... according to the error message it seems like you are using something like jsoneditoronline.org (which uses jsonlint) for testing ... maybe the parsing library you are using within c# understands your given JSON better ... Please also see Ruzihm's answer where he already posted a possible solution in c# as well
  • stackoverflow.com/questions/58407055/… This was my problem before adding [..... ]
  • Note that instead of Application.persistentDataPath + "/" + savedName+".json" you should rather always go for Path.Combine(Application.persistentDataPath, savedName+".json")