Javascript - 如何将每个json对象解析为文本字符串

时间:2022-01-20 13:41:26

I have my html working with javascript, and the file manifest.json to be read.

我的html使用javascript,并且要读取manifest.json文件。

Here is my json file's code:

这是我的json文件的代码:

{
    "name": "Project",
    "description": "A template project",
    "icon": "img/icon.svg",
    "version": "X.X.X.X",
    "developer": {
        "name": "Daniell Mesquita",
        "url": "http://about.me/daniellmesquita"
    },
    "shop_id": "1",
    "manifest_version": 1,
    "default_language": "en"
}

I need read each object as text, parsing in javascript. Example:

我需要将每个对象作为文本读取,在javascript中解析。例:

<h1>+ name +</h1>
<h2>+ description +</h2>
<img src="+ icon +"/>
<p>+ version +</p>

Too, how can I read sub-objects of "developer" object?

太,我怎么能读取“开发者”对象的子对象?

EDIT: Validated json. Thanks to @ADreNaLiNe-DJ and Curious Concept's JSON Formater for helping validate.

编辑:验证json。感谢@ ADreNaLiNe-DJ和Curious Concept的JSON Formater帮助验证。

3 个解决方案

#1


0  

You might do like the following. It will get your object key value pairs flattened even if you have nested objects in your data structure.

您可能会喜欢以下内容。即使您的数据结构中有嵌套对象,它也会使对象键值对变平。

var data = {
                       "name": "Project",
                "description": "A template project",
                       "icon": "img/icon.svg",
                    "version": "X.X.X.X",
                  "developer":
                             {
                              "name": "Daniell Mesquita",
                               "url": "http://about.me/daniellmesquita"
                             },
                    "shop_id": "1",
           "manifest_version": "1",
           "default_language": "en",
           },
stack = [[],[]];

function getKeyValuePairs(obj,stack) {
  Object.keys(obj).forEach( e => obj[e] instanceof Object ? getKeyValuePairs(obj[e], stack) : (stack[0].push(e), stack[1].push(obj[e])));
}
getKeyValuePairs(data, stack);

document.write("<pre>" + JSON.stringify(stack,null,2) + "</pre>");

#2


0  

If the json is in a String variable convert it to an object. Then you can access the single values:

如果json在String变量中,则将其转换为对象。然后您可以访问单个值:

var obj = JSON.parse(jsonString);

var name = obj.name;
var version = obj.version;
var developerName = obj.developer.name;

#3


0  

Well you can use yourJsonVarName.developer.name yourJsonVarName.developer.url

那么你可以使用yourJsonVarName.developer.name yourJsonVarName.developer.url

try this!

Added

If your external file is in your domain maybe you can use this to retrieve your data inside json file.

如果您的外部文件在您的域中,也许您可​​以使用它来检索json文件中的数据。

    $.getJSON( "manifest.json", function( data ) {
        var name = data.developer.name;
        var url = data.developer.url;
    });

#1


0  

You might do like the following. It will get your object key value pairs flattened even if you have nested objects in your data structure.

您可能会喜欢以下内容。即使您的数据结构中有嵌套对象,它也会使对象键值对变平。

var data = {
                       "name": "Project",
                "description": "A template project",
                       "icon": "img/icon.svg",
                    "version": "X.X.X.X",
                  "developer":
                             {
                              "name": "Daniell Mesquita",
                               "url": "http://about.me/daniellmesquita"
                             },
                    "shop_id": "1",
           "manifest_version": "1",
           "default_language": "en",
           },
stack = [[],[]];

function getKeyValuePairs(obj,stack) {
  Object.keys(obj).forEach( e => obj[e] instanceof Object ? getKeyValuePairs(obj[e], stack) : (stack[0].push(e), stack[1].push(obj[e])));
}
getKeyValuePairs(data, stack);

document.write("<pre>" + JSON.stringify(stack,null,2) + "</pre>");

#2


0  

If the json is in a String variable convert it to an object. Then you can access the single values:

如果json在String变量中,则将其转换为对象。然后您可以访问单个值:

var obj = JSON.parse(jsonString);

var name = obj.name;
var version = obj.version;
var developerName = obj.developer.name;

#3


0  

Well you can use yourJsonVarName.developer.name yourJsonVarName.developer.url

那么你可以使用yourJsonVarName.developer.name yourJsonVarName.developer.url

try this!

Added

If your external file is in your domain maybe you can use this to retrieve your data inside json file.

如果您的外部文件在您的域中,也许您可​​以使用它来检索json文件中的数据。

    $.getJSON( "manifest.json", function( data ) {
        var name = data.developer.name;
        var url = data.developer.url;
    });