如何串联json值?

时间:2019-04-15 13:20:40

标签: c# json xamarin.forms json.net

我有一个带有一些字符串数据的json。 json解析和获取值工作正常,但是我想要做的是将json值之间的连接如下所示,是我的json数据

{
"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."
    ]
}

}

我想对我的json数据执行此操作,例如以下我要连接值的字符串列表

 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?"

所以我只想使用下面的json数据执行此操作

    {
    "Addition": {
        "Easy": [

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

这是我将json作为字符串读取的代码,然后我调用for循环从json中选择一些随机字符串,并将其值存储在 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]]);


                    }

2 个答案:

答案 0 :(得分:0)

也许这样的事情可能为您提供解决方案:

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))
};

答案 1 :(得分:0)

鉴于您有一份附录清单,因此必须在从Json获得的List<string>前面附加附录。您可以像使用Linq Select那样简单地完成此操作:

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();

使用rand.Next(N)的int在0到N-1之间。

如果结果确实必须是Json,并且您不能直接在修改后的列表上工作:

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);

演示:https://dotnetfiddle.net/UeWuFp