简体   繁体   English

在unity3d中使用LitJson

[英]Using LitJson in unity3d

2 questions. 2个问题。

1) Is it possible to use the LitJson library as is while scripting in Javascript? 1)是否可以在使用Javascript编写脚本时按原样使用LitJson库? This is really a general question about being able to use c# source in javascript source. 这实际上是一个关于在JavaScript源代码中使用c#源代码的普遍问题。

2) I'm new to c# dev. 2)我是C#开发人员的新手。 I can't seem to get LitJson up and running. 我似乎无法启动LitJson并使其运行。 The following code throws this error: An object reference is required to access non-static member LitJson.JsonReader.Read()'` 以下代码引发此错误: An object reference is required to access non-static member LitJson.JsonReader.Read()' An object reference is required to access non-static member

using UnityEngine;
using System.Collections;

public class Loadr : MonoBehaviour {
string url= "http://developer.echonest.com/api/v4/artist/images?api_key=N6E4NIOVYMTHNDM8J&id=ARH6W4X1187B99274F&format=json&results=1&start=0&license=unknown";
void  Start (){
    WWW www = new WWW(url);
    print(www.text);
    Object a = LitJson.JsonReader.Read(www.text);
    print(a.response.status);

    }

}

any thoughts? 有什么想法吗?

I've been through a bunch of Json libraries in Unity3D. 我经历过Unity3D中的一堆Json库。 The only one I've found that doesn't have weird serialization/deserialization quirks, and works consistently in iOS, WebPlayer, and Editor is JsonFX JsonSerializer 1.4 . 我发现的唯一一个没有怪异的序列化/反序列化怪癖,并且在iOS,WebPlayer和Editor中都能正常工作的JsonFX JsonSerializer 1.4

That said, I think your problem is that you have to instantiate a JsonReader before you can use it's methods. 就是说,我认为您的问题是必须先实例化JsonReader,然后才能使用它的方法。

Something like this might work: 这样的事情可能会起作用:

var reader = new LitJson.JsonReader();
Object a = reader.Read(www.text);

Edit: 编辑:

Whoops, I skipped part of your question. 糟糕,我跳过了部分问题。 You should be able to using any managed assembly from UnityScript, just like from C#. 您应该能够使用UnityScript中的任何托管程序集,就像C#中一样。 Instead of including namespaces with using , you use import . 您可以使用import ,而不是在using中包含名称空间。 After that the code would be virtually identical to the C# version, except for the way UnityScript defines types ( var blah : Type = YadaYada(); ) 之后,代码几乎与C#版本相同,除了var blah : Type = YadaYada();定义类型的方式( var blah : Type = YadaYada();

Have a look at the LitJSON fork for Unity3D: 看看Unity3D的LitJSON分支:


Actually LitJson has some issues and is not always working as expected, so I created a simple JSON-Lib that can handle all Unity3D Types, Enums and arbitrary classes. 实际上,LitJson存在一些问题,并非总是能按预期运行,因此我创建了一个简单的JSON-Lib,它可以处理所有Unity3D类型,枚举和任意类。

Find it at GitHub: Tiny-JSON 在GitHub上找到它: Tiny-JSON

You can use it very simple: 您可以非常简单地使用它:

// encode
Animal a = new Animal(4);
string json = Json.Encode(a);

// decode
IList<Animal> a = Json.Decode<IList<Animal>>("[{\"legs\":4}, {\"legs\":2}]");

About question 关于问题

  1. if you want to access js code in c# script ,you should place the js code int Plugins folder, and vice versa. 如果要在c#脚本中访问js代码,则应将js代码放入“ Plugins”文件夹,反之亦然。

  2. Object a = LitJson.JsonReader.Read(www.text);
    I think JosnReader is not a static class,you should instance a new JsonRead Object and then you can access Read(string data) mehotd 我认为JosnReader不是静态类,您应该实例化一个新的JsonRead Object,然后可以访问Read(string data)mehotd

Hm. Have you tried Jayrock ? 你尝试过杰罗克吗? Its code looks OK, but there isn't any documentation to speak of, and the author is satisfied to leave it that way. 它的代码看起来还可以,但是没有任何文档可谈,作者很满意能保留这种方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM