如何组合两个 JSON 字符串存储在数据库中

发布于 2024-10-30 16:41:09 字数 287 浏览 1 评论 0原文

我有以下数据,这些数据实际上应该是一个 JSON 字符串,但我不确定将两者组合成一个有效的 JSON 字符串。我认为睡眠不足使我无法正常思考。

{“0”:{“a”:“22”,“b”:“44”,“b”:“77”}} {“1”:{“a”:“2200”,“b”:“4400”,“c”:“7700”}}

我真的希望它是这样的:

{“0”:{“a”: "22","b":"44","b":"77"},"1":{"a":"2200","b":"4400","c":"7700"} (我假设这是一个有效的 JSON 字符串

I have the following data that really should be one JSON string however I am not sure to combine the two into a valid JSON string. I think lack of sleep is making me not think properly.

{"0":{"a":"22","b":"44","b":"77"}}
{"1":{"a":"2200","b":"4400","c":"7700"}}

I really want it to be something like:

{"0":{"a":"22","b":"44","b":"77"}, "1":{"a":"2200","b":"4400","c":"7700"}} (i am assuming this is a valid JSON string

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

盗琴音 2024-11-06 16:41:09

如果我理解你的问题, http://jsfiddle.net/4S2wC/ 应该是你要找的正确地

http://jsfiddle.net/4S2wC/ should be what you're looking for, if I understand your question correctly

氛圍 2024-11-06 16:41:09
  1. 将两个字符串都转换为对象
  2. 使用 for (var foo in bar) 循环一个对象的所有属性,并复制到另一个对象
  3. 转换回字符串
  1. Convert both strings to objects
  2. Use for (var foo in bar) to loop over all the properties of one object, and copy to the other object
  3. Convert back to a string
影子的影子 2024-11-06 16:41:09

您可以尝试使用正则表达式,例如:

mystring.replace(/}}\s*{/g,"},");

这对对象图的深度和语法做出了一些假设。

You could try a regex such as:

mystring.replace(/}}\s*{/g,"},");

This makes some assumptions around the depth of the object graph and the syntax.

无人问我粥可暖 2024-11-06 16:41:09

下面的函数有助于组合 json 对象。

C#:
        public String toJSONCombine(JSONObject outer, int HVal, int Aval , int Bval , int Cval)
        {    
             JSONObject inner = new JSONObject();
             try {
              inner.put("a", Aval);
              inner.put("b", Bval);
              inner.put("C", Cval);
              outer.put(HVal, inner);
             } 
             catch (JSONException ex) {
               ex.printStackTrace();
             }
        }

你可以像最后那样形成json字符串,

{"0":{"a":"22","b":"44","b":"77"}, "1":{"a":"2200","b":"4400","c":"7700"}}

我想你想要js。我们可以使用 javascript 使其具有相同的结构..

使用数组而不是 JsonObject..

我希望它对您有帮助

Below function is helps to combine the json object.

C#:
        public String toJSONCombine(JSONObject outer, int HVal, int Aval , int Bval , int Cval)
        {    
             JSONObject inner = new JSONObject();
             try {
              inner.put("a", Aval);
              inner.put("b", Bval);
              inner.put("C", Cval);
              outer.put(HVal, inner);
             } 
             catch (JSONException ex) {
               ex.printStackTrace();
             }
        }

You can form the json string like that at end

{"0":{"a":"22","b":"44","b":"77"}, "1":{"a":"2200","b":"4400","c":"7700"}}

I think you want to js. We can make it same Structure using javascripts..

Using array instead of JsonObject..

I hope its help to you

土豪 2024-11-06 16:41:09

为什么人们总是创建自己的功能,这真的超出了我的理解。
有很多功能可供您使用,为什么不使用它们呢?
Jquery 有一些非常好的函数,例如

$.extend({},j1,j2) http://api.jquery.com/jQuery.extend/

$.merge( [0,1,2], [2,3,4] )对于数组边距 http://api.jquery.com/jQuery.merge/

在此处输入图像描述

Why people create their own function always, it's really out of my mind.
There are lot's of functions out there for you, why not use those?
Jquery has some really good functions about these, like

$.extend({},j1,j2) http://api.jquery.com/jQuery.extend/

$.merge( [0,1,2], [2,3,4] ) for array marge http://api.jquery.com/jQuery.merge/

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文