用另一个数组c#的元素替换json数组元素

时间:2016-06-02 07:40:14

标签: c# json

我有两个json字符串..

string days = "['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']";
string data= "[[0,0,0],[2,1,0],[0,2,0],[3,3,0],[0,6,0],[0,7,0],[5,8,0],[0,9,1],[6,10,3],[0,11,5],[0,12,4],[4,13,7],[0,14,1]]";

我想将json数据转换为..

表示所有数组,如[0,0,0]

a)如果最后一个值为0,则从数据中删除该数组

b)将第一个值替换为days数组中的第一个值 例如...... [3,13,7]变为[“星期三”,第13,7]

我从这开始......

JArray daysarr = JArray.Parse(days);
JArray myarr = JArray.Parse(data);
foreach (var myobj in myarr)
{
    // what do I do here????
}  

真心感谢任何帮助。

由于

4 个答案:

答案 0 :(得分:3)

这个解决方案怎么样:

var js = new JavaScriptSerializer();
string days = "['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']";
string data = "[[0,0,0],[2,1,0],[0,2,0],[3,3,0],[0,6,0],[0,7,0],[5,8,0],[0,9,1],[6,10,3],[0,11,5],[0,12,4],[4,13,7],[0,14,1]]";

var Days = js.Deserialize<List<string>>(days);
var Data = js.Deserialize<List<List<int>>>(data);

var answer = js.Serialize(Data.Where(x => x.Last() != 0).ToList().Select(x => {
    var list = new List<object> { Days[x.First()] };
    list.AddRange(x.Skip(1).Select(y => (object)y).ToList());
    return list;
}).ToList());

答案 1 :(得分:1)

 string days = "['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']";
        string data = "[[0,0,0],[2,1,0],[0,2,0],[3,3,0],[0,6,0],[0,7,0],[5,8,0],[0,9,1],[6,10,3],[0,11,5],[0,12,4],[4,13,7],[0,14,1]]";

        JArray daysarr = JArray.Parse(days);
        JArray myarr = JArray.Parse(data);

        JArray result = new JArray();


        foreach (var myobj in myarr)
        {
            if (myobj.Last.Value<int>() != 0)
            {
                var t = new object[3];
                t[0] = daysarr[myobj.First.Value<int>()];
                t[1] = myobj[1];
                t[2] = myobj[2];

                result.Add(t);
            }
        }

答案 2 :(得分:1)

foreach循环是只读的(您无法修改正在迭代的集合,例如删除项目等); 你可以在你的情况下使用for循环,你需要一个可以存储字符串和整数的新容器,例如List<Tuple<object, int, int>>

您还需要if声明:

JArray daysarr = JArray.Parse(days);
JArray myarr = JArray.Parse(data);

var result = new List<Tuple<object, int, int>>();

foreach(myobj in myarr)
{
    if(myobj[2] != 0)
    {
        var newValue = new Tuple<object, int, int>(daysarr[myobj[0]], myobj[1], myobj[2]);
        result.Add(newValue);
    }
} 

答案 3 :(得分:0)

您可以尝试以下代码:

string days = "['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']";
string data = "[[0,0,0],[2,1,0],[0,2,0],[3,3,0],[0,6,0],[0,7,0],[5,8,0],[0,9,1],[6,10,3],[0,11,5],[0,12,4],[4,13,7],[0,14,1]]";

JArray daysarr = JArray.Parse(days);
JArray myarr = JArray.Parse(data);
// a) If the last value is 0 remove this array from data
// b) Replace the first value with the first value in days array eg ..[3,13,7] becomes ["Wednesday",13,7]
// This result array we used for storing all valid data which follow the two rules as define above comment.
JArray result = new JArray();
foreach (var myobj in myarr)
{
    if (myobj.Count() == 3)
    {
        var firstValue = Convert.ToInt32(myobj[0]);
        var secondValue = Convert.ToInt32(myobj[1]);
        var thirdValue = Convert.ToInt32(myobj[2]);

        // Here we check last value of current array
        if (thirdValue != 0)
        {
            string day = Convert.ToString(daysarr[firstValue]);

            myobj[0] = day;

            result.Add(myobj);

        }
    }
}