0

I have two json strings..

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]]";

I would like to convert the json data such that..

for all array such as [0,0,0]

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]

I started with this..

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

Any help is sincerely appreciated..

Thanks

0

4 Answers 4

3

What about this solution:

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());
Sign up to request clarification or add additional context in comments.

2 Comments

thanks.. could you please explain what is happening in var list
I create list of objects, because you combine inside array string and int types(they should be cast to common type) and initialize it with the help of days array and first number as index: Days[x.First()]. Then I insert list.AddRange into this list remaining numbers(also casted to object type: (object)y) - all of them except first element(Skip(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);
            }
        }

Comments

1

foreach loop is read-only (you can't modify the collection that's being iterated, such as removing items, etc.); you can use a for loop instead in your case, you'll need a new container that can store both strings and ints, such as List<Tuple<object, int, int>>.

You'll also need an if statement:

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

Comments

0

Could you try this below code:

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

        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.