【问题标题】:JSON Objects merge issue from an Object Array来自对象数组的 JSON 对象合并问题
【发布时间】:2016-09-04 14:22:50
【问题描述】:

逻辑是在按钮单击事件中编写的,该事件获取对象数组。在 ForEach 循环中,我将每个对象转换为 JSON,但我无法将其合并为最终的 json 对象。我收到错误,因为无法通过实例引用访问 'string.Concat(params string[])';改为使用类型名称对其进行限定。

protected void Button2_Click(object sender, EventArgs e)
{

String json = "";

String jsonoutput = "";

foreach (SAFWebReference.Usagr value in response.PRoles)

        {

           json = new JavaScriptSerializer().Serialize(value);
           jsonoutput = jsonoutput.Concat(json);

        }
}

【问题讨论】:

  • 你不能连接 json 字符串来形成一个有效的 json...序列化 response.PRoles ....{a:1} 是一个有效的 json 但{a:1}{a:2} 不是。 ..
  • 使用 jsonoutput = jsonoutput + json;如果它只是纯粹用于合并而不是最后的有效 json
  • ...而Concatstatic 方法,因此您不能像在实例上那样使用它。
  • 为什么你的respnse.PRoles不能直接序列化?
  • 似乎你想要jsonoutput = string.Concat(jsonoutput, json);,但我认为你走的不是正确的道路......

标签: c# arrays json merge push


【解决方案1】:

您需要序列化完整的列表对象,而不是一次序列化一个项目并尝试手动合并字符串。

var json = new JavaScriptSerializer().Serialize(response.PRoles);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-28
    • 1970-01-01
    • 2022-01-02
    • 2018-10-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多