如何将值从json字符串存储到数组 [英] How to store a value from an json string to array

查看:93
本文介绍了如何将值从json字符串存储到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我正在尝试从json取值到数组,但是在某个地方我出错了.它正在运行,但不能完全使用.

我的JSON字符串是:{{"WorkingDays":星期一,星期二,星期三"},{"WorkingTime":"10 AM:7 PM,11 AM:2 PM,4 PM:4 AM"}}

我的业务逻辑中的代码如下:

Hi,


I am trying to fetch the value from json into array but somewhere i am going wrong. It is working but not fully.

My JSON String is : {{"WorkingDays":"Monday, Tuesday, Wednesday"},{"WorkingTime":"10 AM : 7 PM, 11 AM : 2 PM, 4 PM : 4 AM"}}

The code in my business logic is as follows :

public string wsOutletRegistration(string str)
{

  string[] O_WorkingDays = null;
  string[] O_WorkingTime = null;
  string WorkingTime = string.Empty;

  String[] para = str.Split('{', '}', '[', ']', ':', '"');

  for (int i = 0; i < para.Length; i++)
  {
    if (para[i].Equals("WorkingDays"))
                    WorkingDays = para[i + 3];
                else if (para[i].Equals("WorkingTimeFrom"))
                //WorkingTime = para[i + 3];
                {
                    string[] from = {"", "", "", ""," ", ""};
                    string[] to = { "", "", "", "", "", "" };
                    int k = 0;
                    int h = 0;
                    WorkingTime = para[i + 1];
                    for (int j = (i + 3); j < para.Length - 1; j++)
                        WorkingTime = WorkingTime + para[j];
                    O_WorkingTime = WorkingTime.Split(' ', ',');
                    for (int j = 0; j < O_WorkingTime.Length; j++)
                    {
                        from[k] = O_WorkingTime[j] + O_WorkingTime[j + 1];
                        to[k] = O_WorkingTime[j + 3] + O_WorkingTime[j + 4];
                        j = j + 2;
                        k++;
                    }
                }
  }



我想要得到的是from [k]应该包含10 AM、11AM、4PM之类的值,而to [k]应该包含7 PM、2PM和4AM之类的值.

但是我得到的结果是[k] = 10 AM、7PM、11AM、2PM、4PM、4AM
并改为[k] = 7 PM、11AM、2PM、4PM、4AM.

因此,我需要在代码中进行更改,以便获得所需的值.请帮帮我.

在此先感谢.....



What i want to get is from[k] should contain the values like 10AM, 11AM, 4PM and to[k] should contain 7PM , 2PM and 4AM.

But i am getting the result as from[k] = 10AM, 7PM, 11AM, 2PM, 4PM, 4AM
and to[k] = 7PM, 11AM, 2PM, 4PM, 4AM.

So what i am needed to change in my code so that i can get the values i am expecting. Please help me out.

Thanks in advance.....

推荐答案

这是可怕的代码.就像其他人说的那样,您应该使用可以正确解析JSON的库,但是,请勿创建空数组,并且ASSUME传入的数组大小相同.使用List类,并在需要时最后调用ToArray.

如果必须对此进行解析,我将在上拆分,读取每个字符串,在:上拆分,然后将这两个值放入两个列表中.您的代码现在是如此混乱,我不确定为什么会出错或如何运行.
This is horrible code. Like someone else said, you should use a library that parses JSON properly, but, don''t create an empty array and ASSUME the array coming in is the same size. Use the List class, and call ToArray at the end if you need to.

If I had to parse this, I''d split on the , the read each string, split on the : and put those two values in to the two lists. Your code is so convoluted right now, I''m not sure why it''s going wrong, or how it''s running.


这篇关于如何将值从json字符串存储到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆