如何在textaea中查找类和方法,并使用PHP或js保存为json [英] How to find classes and methods in textaea and save as json with PHP or js

查看:86
本文介绍了如何在textaea中查找类和方法,并使用PHP或js保存为json的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我用PHP开发了一个Web应用程序用户必须能够导入一个程序,这些数据应该作为JSON存储在数据库中。 />
例如我在Textarea中写了两个类java:

 public class a {

private int a1;
private int a2;
private static int a3 = 0;

public a(int start,int end){
// code
}

//返回ID实例变量$ b的新方法$ b public int getID(){
return id;
}
公共类b {

private int b1;
private int b2;
private static int b3 = 0;

public b4(int b5,int b6){
// code
}

public int getID(){
return id ;
}
}





我想像Json这样得到这些数据:



 [
{
class:a,
属性:private int a1;
private int a2;
private static int a3 = 0;
方法:
{

[
{name:public a(int start,int end),code:.. 。},
{name:public int getID(),code:return id; },

]
},

},
{
class:b,
属性 :private int b1; private int b2; private static int b3 = 0;
method:
{

[
{name: public b4(int b5,int b6),code:...},
{name:public int getID(),code:return id;},

]
},
}





我尝试了什么:



如何用PHP或Js做?

解决方案

这似乎就像你试图获取类信息一样,例如通过反射。因为,在大多数情况下,JSON不会那样,相反它只包含一些信息来重新创建对象,就像这样,

 {
a1:< span class =code-digit> 123 ,
a2 321
a3 312
}

JSON数据,不包含方法信息,或者他们如何在类实现中编写。为此,您可以轻松使用JavaScript JSON对象来序列化运行时对象(仅适用于JavaScript,不是Java !),

  var  obj =  JSON  .stringify(yourobj);  //   {a1:123 ...}  



但我相信,你也想要使用对象的元数据,因为你需要学习Java Reflection API来获取有关方法签名的信息,而且它的主体将非常困难并且可能还需要源代码本身。 你为什么要这样做?



最后,PHP似乎被遗漏了,因为那部分只会被用来想要编写一个实际上允许用户输入他们的Java程序以转换为JSON表示法的网站。



Trail:The Reflection API(The Java™Tutorials) [ ^ ]

JSON.stringify() - JavaScript | MDN [ ^ ]


Hi everyone,
I have developing a web-application with PHP And users must be able to import a piece of their programs and these data should be stored in the database as a JSON.
As example I write into Textarea two classes java like this :

public class a {
        
    private int a1;
    private int a2;
    private static int a3 = 0;
        
    public a(int start, int end){
        // code 
    }

    // new method to return the ID instance variable
    public int getID() {
        return id;
    }
        public class b {
        
    private int b1;
    private int b2;
    private static int b3 = 0;
        
    public b4(int b5, int b6){
        // code 
    }

    public int getID() {
        return id;
    }
}



I want to get this data as Json like this :

[
	{
		"class": "a",
		"attribute":"private int a1;
					private int a2;
					private static int a3 = 0;"
		"method":
			{
				
					[
						{ "name": "public a(int start, int end)", "code": "..." },
						{ "name": "public int getID()", "code": "return id;" },
						
					]
			},
		
	},
	{
		class": "b",
		"attribute":"private int b1;private int b2;private static int b3 = 0;"
		"method":
			{
				
					[
						{ "name": "public b4(int b5, int b6)", "code": "..." },
						{ "name": "public int getID()", "code": "return id;" },
						
					]
			},
	}



What I have tried:

how can I do it with PHP or Js?

解决方案

That seems like you are trying to get the class information such as through reflection. Because, in most cases the JSON won't be like that, instead it would only contain a bit of information to recreate the objects, something like this,

{
   "a1": 123,
   "a2": 321,
   "a3": 312
}

JSON data, does not contain the method information, or how do they get written in the class implementation. For this, you can easily use JavaScript JSON object, to serialize the runtime objects (only for JavaScript, not the Java!),

var obj = JSON.stringify(yourobj); // { "a1": 123... }


But I believe, you want to use the object's metadata as well, for that you would require to learn the Java Reflection APIs to fetch the information about a method's signature, and its body would be much difficult and might also require the source code itself. Why are you even doing this?

Finally, PHP seems to be left out, because that part is only going to be used if you want to to write a website that actually allows the users to enter their Java programs to be converted to JSON notation.

Trail: The Reflection API (The Java™ Tutorials)[^]
JSON.stringify() - JavaScript | MDN[^]


这篇关于如何在textaea中查找类和方法,并使用PHP或js保存为json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆