格式化我的json

时间:2017-06-19 05:00:25

标签: python json

我开发了一个Python脚本,可以将json数据相互附加,希望创建多个json对象。但是,这样做并没有正确格式化,我不确定如何格式化我的json来创建多个json对象。

internal class NavigationRegistration : Module
{
    /// <summary>
    /// Load the navigation related types into the given builder.
    /// </summary>
    /// <param name="builder">Container Builder that will be turned into the final IOC Container</param>
    protected override void Load(ContainerBuilder builder)
    {
        // Register the NavigationPage in Xamarin with the Prism Navigation system.
        //builder.RegisterType<NavigationPage>().AsSelf();
        //PageNavigationRegistry.Register(nameof(NavigationPage), typeof(NavigationPage));

        // Get all of the Types that represent a View in our assembly for DI and navigation registration
        // If start-up time ever becomes an issue, we can replace this assembly scan with explicit registrations.
        Type[] viewTypes = base.ThisAssembly.GetTypes().Where(type => type.IsAssignableTo<Page>()).ToArray();

        // Iterate over each discovered View Type and register it with the navigation system and DI container.
        for(int index = 0; index < viewTypes.Length; index++)
        {
            Type viewType = viewTypes[index];
            builder.RegisterType(viewType).Named<Page>(viewType.Name);

            // If we ever need to disconnect a view name from its Type name, we can do so here.
            // We would build a custom attribute to decorate the view with, pull the attribute from the Type
            // and register the Type with the attribute value.
            PageNavigationRegistry.Register(viewType.Name, viewType);
        }
    }
}

将这些数据复制到json验证器会给我带来错误,而它却期望有一个EOF。但是,如何修改上述数据以创建多个json对象,从而验证我的json?

3 个答案:

答案 0 :(得分:2)

JSON 总是只有一个对象,beit数组或文字对象,它不能是一堆独立的数组或文字对象。

  

JSON is built on two structures:
   *名称/值对的集合。在各种语言中,这被实现为对象,记录,结构,字典,哈希表,键控列表或关联数组。
   * 有序的值列表。在大多数语言中,这被实现为数组,向量,列表或序列。

假设您发布的对象集合应该是一个JSON,您可以将每个对象作为数组的元素,并像这样传递它。

以下示例使用JavaScript,但希望您能够明白这一点。

&#13;
&#13;
const arr = [
    {"name": "8808", "ip": "192.168.241.110", "cameras": {"front": ["nf091"], "inside": ["nf067"], "right": ["004317"], "rear": ["000189"], "left": ["nf084"]}, "serial": "000002", "simId": 197078302},
    {"name": "8893", "ip": "192.168.241.137", "cameras": {"front": ["nf052"], "inside": ["000211"], "right": ["000069"], "rear": ["000441"], "left": ["000400"]}, "serial": "000277", "simId": 197057802},
    {"name": "1620", "ip": "192.168.242.145", "cameras": {"front": ["000174"], "inside": ["000197"], "right": ["000304"], "rear": ["000295"], "left": ["000553"]}, "serial": "000084", "simId": 310922501},
    {"name": "0632", "ip": "192.168.242.166", "cameras": {"front": ["nf050"], "inside": ["nf022"], "right": ["nf047"], "rear": ["ne056"], "left": ["ne083"]}, "serial": "NF016", "simId": 310897301},
    {"name": "1544", "ip": "192.168.242.234", "cameras": {"front": ["000061"], "inside": ["000068"], "right": ["004440"], "rear": ["000219"], "left": ["005516"]}, "serial": "000200", "simId": 310839901},
    {"name": "12HA", "ip": "192.168.243.116", "cameras": {"front": ["000625"], "right": ["nf104"], "rear": ["ne047"], "left": ["000717"]}, "serial": "000181", "simId": 510339201}
];

var packed_json = JSON.stringify( arr ), // transportable
    json = JSON.parse( packed_json ); // operable

console.log( json[ 1 ].simId ); // for example
&#13;
&#13;
&#13;

答案 1 :(得分:1)

您应该将所有这些放在一个像列表一样的外部容器中。像<!DOCTYPE html> <html> <body> <p>Click the button to sort the array.</p> <button onclick="myFunction()">Try it</button> <p id="demo"></p> <script> var fruits = ["Banana", "Orange", "Apple", "Mango"]; document.getElementById("demo").innerHTML = fruits; function myFunction() { fruits.sort(); document.getElementById("demo").innerHTML = fruits; } </script> </body> </html> 这样的东西。

答案 2 :(得分:1)

一些观察结果:

  • 您必须将所有对象包装到由 (,) 分隔的单个数组中,因为objects的行为类似于array elements
  • 您可以使用ES6 for...of循环或使用ES6 map()方法迭代数组。

有效JSON:

[{"name": "8808", "ip": "192.168.241.110", "cameras": {"front": ["nf091"], "inside": ["nf067"], "right": ["004317"], "rear": ["000189"], "left": ["nf084"]}, "serial":     "000002", "simId": 197078302},
{"name": "8893", "ip": "192.168.241.137", "cameras": {"front": ["nf052"], "inside": ["000211"], "right": ["000069"], "rear": ["000441"], "left": ["000400"]}, "serial":     "000277", "simId": 197057802},
{"name": "1620", "ip": "192.168.242.145", "cameras": {"front": ["000174"], "inside": ["000197"], "right": ["000304"], "rear": ["000295"], "left": ["000553"]}, "serial":     "000084", "simId": 310922501},
{"name": "0632", "ip": "192.168.242.166", "cameras": {"front": ["nf050"], "inside": ["nf022"], "right": ["nf047"], "rear": ["ne056"], "left": ["ne083"]}, "serial":     "NF016", "simId": 310897301},
{"name": "1544", "ip": "192.168.242.234", "cameras": {"front": ["000061"], "inside": ["000068"], "right": ["004440"], "rear": ["000219"], "left": ["005516"]}, "serial":     "000200", "simId": 310839901},
{"name": "12HA", "ip": "192.168.243.116", "cameras": {"front": ["000625"], "right": ["nf104"], "rear": ["ne047"], "left": ["000717"]}, "serial": "000181", "simId":     510339201}]

enter image description here