php 如何将一个 JSON 包含到另一个 JSON 中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22216638/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to include one JSON into another JSON?
提问by sreenath
My first json is: this will come from java url, here i am using php, in php iam calling java url.
我的第一个 json 是:这将来自 java url,这里我使用的是 php,在 php 中调用 java url。
{
"categoryId":"Painting",
"subCategoryId":"Residential",
"alternatives": [1,2,3,4,5],
"criterias":["price","quantity","yom","company"],
"answerss":[["1000","12343","4543","","4645646"],["12","23","34","","45"],["2014","","2000","1990","2005"],["xyz","","Abc","jkl","mno"]]
}
My Second JSON is: this iam generating using jquery.
我的第二个 JSON 是:这是使用 jquery 生成的。
{"criterias":"Location"}
How can i include second JSON into first JSON criterias.
我如何将第二个 JSON 包含到第一个 JSON 标准中。
采纳答案by Purushotham
You need to convert your first JSON string to object and add a new property to it.
您需要将第一个 JSON 字符串转换为对象并向其添加新属性。
If it is Java, you can use Google's Gsonlibrary
如果是Java,可以使用谷歌的Gson库
Gson gson = new Gson();
JsonObject jsonObj = gson.fromJson (jsonStr, JsonElement.class).getAsJsonObject();
jsonObj.add("criterias", "Location");
If it is JavaScript,
如果是 JavaScript,
var jObj = JSON.parse(jsonString);
jObj.criterias = "Location"; //jObj.NewItem = "NewValue";
回答by PressingOnAlways
I believe the original question is how to merge these 2 JSON objects within javascript. Assuming that, the answer is simple.
我相信最初的问题是如何在 javascript 中合并这 2 个 JSON 对象。假设,答案很简单。
var firstJson = {
"categoryId":"Painting",
"subCategoryId":"Residential",
"alternatives": [1,2,3,4,5],
"criterias":["price","quantity","yom","company"],
"answerss":[["1000","12343","4543","","4645646"],["12","23","34","","45"],["2014","","2000","1990","2005"],["xyz","","Abc","jkl","mno"]]
};
var secondJson = {"criterias":"Location"};
firstJson.criterias = $.extend({},firstJson.criterias,secondJson.criterias);
回答by Ildar Rakhmanov
The simplest way to include one file in another is to use cpp (C preprocessor).
将一个文件包含在另一个文件中的最简单方法是使用 cpp(C 预处理器)。
For example, if I have test.json as
例如,如果我有 test.json 作为
{"name":"NAME",
#include "another.json"
}
and another.json as
和 another.json 作为
"inner":"value"
you should be able to obtain
你应该能够获得
$ cpp -P test.json
{"name":"NAME",
"inner":"value"
}
Of course, you will need to make sure that resulting syntax is correct by properly formatting both pieces.
当然,您需要通过正确格式化这两部分来确保生成的语法是正确的。
回答by Jatin patil
Here is the example of how you write nested JSON
这是你如何写作的例子 nested JSON
{"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}}
For More examples visit following links:
有关更多示例,请访问以下链接:

