- Home /
Stop working after for loop appending string
I have totally out of idea what is going on, the final string return nothing or totally stop working after running the following for loop appending string, somebody could help:-
public void JsonToHistory(string json)
{
string recombineJson = "{\"history\":[";
string[] trimForKey = { "\"-", "\":{\"" };
// result in [0] = { and [1] = some index key and [2] = "orderlist..."user_id":"null}," with the last string with even index end with double bracket }}
string[] splitJson = json.Split(trimForKey, StringSplitOptions.RemoveEmptyEntries);
//add {"index":"- to every odd i, and add "," to every even i
for (int i = 1; (i - 1) / 2 < splitJson.Length; i += 2)
{
recombineJson += "{\"order_number\":\"-" + splitJson[i] + "\",\"" + splitJson[i + 1];
//Working fine
Debug.Log("new recombineJson is " + recombineJson);
}
//Stop working...
Debug.Log(recombineJson); // return nothing
recombineJson = recombineJson.TrimEnd('}') + "]}";
}
Probably an IndexOutOfRange exception is thrown due to the fact i goes beyond array's lenght, thus execution of the method stops. Fix the loop.
Answer by danieilng21 · Dec 05, 2018 at 10:19 AM
It works now after I had changed it to 2 for loop -one begin with i=1, i +=2 and another with i=2, i+=2, and join it and modified it. And I had tried with the old code again after that, same problem occur. Thanks anyway.
Your answer
![](https://koobas.hobune.stream/wayback/20220612183244im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Loop through string? (foreach new line) 3 Answers
Generate Json from Class Type List 1 Answer
How to make AudioSource play defined clips using String? 1 Answer
How to get object array from json? 0 Answers
Litjson autoformat 1 Answer