2

i have a json with some string data. json parsing and getting values are working fine but what i want to do is to concatenate between json values like below is my json data

{
"Addition": {
    "Easy": [

        "New York Bulls",
        "Los Angeles Kings",
        "Golden State Warriros",
        "Huston Rocket",
        "I have a problem.",
        "What is the price of this cap?",
        "Sorry, I was sick. I went to bed early.",
        "I'll be back right away",
        "This is my cousin.",
        "I am not always at home on Sundays.",
        "Tom suggested another plan to the committee.",
        "I'll look in the living room.",
        "I came here to see if there was something I could do to help", 
         "but there doesn't seem to be anything for me to do.",
        "You have a very nice car.",
        "John wanted to be completely independent of his family.",
        "Mary is studying in her room.",
        "He used to eat out every day, but now he can't afford it.",
        "He introduced me to a pretty girl.",
        "How many books does he have?",
        "I'll pay.",
        "You don't have to get up so early.",
        "Do you like tennis?",
        "Keep out.",
        "Coach."
    ]
}

}

i want to do like this with my json data like in below list of strings i am concatenating values

 var list =    new List<string> { girl1names[GirlName] +" was playing 
basketball. " + random.Next(min, max) + " of her shots went in the hoop. 
" + random.Next(min, max) + " of her shots did not go in the hoop. How 
many shots were there in total?"

so i just want to do this with my json data like this below

    {
    "Addition": {
        "Easy": [

        " girl1names[GirlName] + New York Bulls",
        " random.Next(min, max) + Los Angeles Kings"
]
}
}

This the code where i am reading my json as a string then i call a for loop pick some random strings from the json and store that values in a wordproblemslist

    using (var reader = new System.IO.StreamReader(streams))
                {

                    string json = reader.ReadToEnd();

                    JObject jObject = JObject.Parse(json);
                    JToken jUser = jObject["Addition"];


                    var result = JsonConvert.DeserializeObject<RootObject> 
(json);
                    List<string> lst = new List<string>();
                    for (int i = 0; i < result.Addition.Easy.Count; i++)
                    {
                        lst.Add(result.Addition.Easy[i]);
                    }
                    List<int> listNumbers = new List<int>();

                    int number;
                    for (int j = 0; j < questions; j++)
                    {


                        do
                        {
                            number = randoms.Next(lst.Count);
                        } while (listNumbers.Contains(number));
                        listNumbers.Add(number);
                    }
                    for (int k = 0; k < listNumbers.Count; k++)
                    {

                        wordproblemslist.Add(lst[listNumbers[k]]);


                    }
Masti Broz Top
  • 89
  • 2
  • 10
  • [mcve], and [ask]. What is `girl1names` and `GirlName`? How are they related to Easy? Do you have an error message? – xdtTransform Apr 15 '19 at 13:31
  • girl1names is a ist which contains some random names actually i am making a word problems application now i have a json data in future these will be questions i just want to concatenate values like i am doing below in my question – Masti Broz Top Apr 15 '19 at 13:33
  • Your question is unclear, there seems to be no connection between the json and the code. And you didn't provide one. I will recommend reading my previous link those guide lines will help you specify your issue in an understandable way. I'm pretty sure it's almost clear and your head and look easy. but for now you are missing some sentence to connect everything. – xdtTransform Apr 15 '19 at 13:39
  • check my question again – Masti Broz Top Apr 15 '19 at 13:39
  • So you want to deserialise a Json modify the property `Easy`, and serialize it back? but why serializing you can simply use the modified version. – xdtTransform Apr 15 '19 at 13:41
  • Json is the serialisation of an object. Deserialize it. change the value of the property serialize it back like [this](https://stackoverflow.com/questions/21695185/change-values-in-json-file-writing-files). – xdtTransform Apr 15 '19 at 13:43
  • look what i am doing is this that i am reading this json file deserializing it and showing its values in a list and then showing that list values in a pdf file now what i want isto modify the properties like add random name or random numbers before every value of Easy – Masti Broz Top Apr 15 '19 at 13:49
  • How do you select the original text you want to concatenate? Why ` girl1names[GirlName]+"New York Bulls"` ? Do you have a list of things to add and you match them using index? – xdtTransform Apr 15 '19 at 13:49
  • But's it's unclear how you decide if you add a number or a girl1names.. – xdtTransform Apr 15 '19 at 13:50
  • yes i have a list of names now i want to do that i will pick some random names from that list and then concatenate them with json data – Masti Broz Top Apr 15 '19 at 13:50
  • let me edit my question – Masti Broz Top Apr 15 '19 at 13:50
  • Well I didn't real your new code but so mutch to say . To make a copy of an existing list youn Don't have to iterate to all element and add to a new list you can simply ask for copy. like [this](https://stackoverflow.com/questions/222598/how-do-i-clone-a-generic-list-in-c). – xdtTransform Apr 15 '19 at 14:18
  • All that comment really underline the need to read those guide line. you can't except people to play this game of question/answer each time you have a question. ^^ – xdtTransform Apr 15 '19 at 14:20
  • And please don't put every thing in that `using`! Stream are like water tap, once you filled your glass with the water you need. Please close the stream, before takeing you time drinking the water. – xdtTransform Apr 15 '19 at 14:24
  • addedum copying a List for simple type that doesn't need clone like `string` => https://stackoverflow.com/questions/1952185/how-do-i-copy-items-from-list-to-list-without-foreachè – xdtTransform Apr 15 '19 at 14:26
  • Most of your for loop will get you a axe kick in any code review. You may want to get a copy of a book like "C# in a nutshell" in order to go over the basic. And alway go for pen and paper of what you want to do. Simple and clear sentence on paper will help you build better code. – xdtTransform Apr 15 '19 at 14:35

2 Answers2

0

Perhaps something like this could be a solution for you:

var list = new List<string>()
{
    string.Format("{0} was playing baskeball. {1} of her shots went in the hoop. How many shots were there in total?", girl1names[GirlName], random.Next(min, max))
};
Saeed Prez
  • 728
  • 3
  • 8
  • no no you are not getting my point sir i dont want to make list i have a json file which contains string data now i can get my string data from json through some code i just want to concatenate – Masti Broz Top Apr 15 '19 at 13:35
  • My point was you create your strings using `{0}`, `{1}` and so on.. that way you can put any values you want in them later with `string.Format()`. Isn't that what you want to do? – Saeed Prez Apr 15 '19 at 13:37
  • check my question again sir i have added what i want – Masti Broz Top Apr 15 '19 at 13:39
0

Given that you have a list of addendum tht you have to append in front of the List<string> you are get from a Json. You can simply do that using Linq Select like :

var appendix = new[] { "1", "2", "3", "Test" };

string input = @"
{
  ""Addition"": {
    ""Easy"": [
       ""Foo"",
       ""Bar""
]}}";


var item = JsonConvert.DeserializeObject<RootObject>(input);
var newEasy = item.Addition
                  .Easy
                  .Select(x => appendix[rand.Next(appendix.Length)] + " " + x).ToList();

Using rand.Next(N) To have an int between 0 and N-1.

If the result really has to be a Json, and you cannot work directly on the modified list :

var item = JsonConvert.DeserializeObject<RootObject>(input);


for (int i = 0; i < item.Addition.Easy.Count(); i++)
{
    item.Addition.Easy[i] = appendix[rand.Next(appendix.Length)] + " " + item.Addition.Easy[i];
}


var jsonResult = JsonConvert.SerializeObject(item, Formatting.Indented);
Console.WriteLine(jsonResult);

Demo : https://dotnetfiddle.net/UeWuFp

xdtTransform
  • 1,986
  • 14
  • 34