0

I'm creating a mobile app in which I use a webservice written in C# to create JSOn which gets sent to the app written in Java, in C# on the website, I use the following to create a new object:

new { success = true }

However in Java I can't seem to do the same, what's the best alternative for this in java if I can't do the same?

To put this into context, here is one of my webmethods on the website:

[WebMethod]
public object getNode(int intId)
{
    DynamicNode node = new DynamicNode(intId);

    object page = new 
    {
        id = node.Id,
        parentId = node.Parent.Id,
        name = node.Name,
        title = node.GetPropertyValue("title"),
        summary = node.GetPropertyValue("summary"),
        body = node.GetPropertyValue("body"),
        updateDate = node.UpdateDate.ToString(),
        createDate = node.CreateDate.ToString()
    };

    return page;
}

If anyone could help me out, I'd be very greatful!

Thanks in advance.

Luke Alderton
  • 3,198
  • 1
  • 23
  • 34
  • I'm not sure of the names though so google is useless to me. – Luke Alderton Apr 15 '13 at 21:24
  • http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html – Simon Apr 15 '13 at 21:31
  • You seem to be missing my point entirely. I don't want to create a new instance of an existing object I want to create a new object, for example the data in the object changes too much to abide by a single predefined class. – Luke Alderton Apr 15 '13 at 21:32
  • In Java, you're better off using a `Map`. You could create an anonymous Object, but you wouldn't be able to access its fields. – Sotirios Delimanolis Apr 15 '13 at 21:34
  • I've read about Hashmap and Hashtable, are these what you refer to? – Luke Alderton Apr 15 '13 at 21:36
  • Yes, you'll store the name of the field as the key and the object of any type as the value. Either one will work, but [you should know the differences](http://stackoverflow.com/questions/40471/differences-between-hashmap-and-hashtable) – Sotirios Delimanolis Apr 15 '13 at 21:37
  • Do you really mean JavaSCRIPT? Or are you looking for the equivalent Java code... because it's almost the same, you just need to call a real constructor instead of the object initializer in your sample code (that's not JSON in your sample, it's a C# object initializer). http://dotnetbyexample.blogspot.com/2008/01/compacting-c-code-using-implicit.html – Jasmine Apr 15 '13 at 21:38
  • @Jasmine From his clarifications, I think he means an anonymous object, which I don't think is very useful in this case. – Sotirios Delimanolis Apr 15 '13 at 21:39
  • @Jazmine I completely understand its C# but the webservice serialises it into JSON and sends it to my mobile app when requested. – Luke Alderton Apr 15 '13 at 21:40
  • Knowing the architecture might help - I'm imagining a JAVA app running on Android, with a C# web service in the back end? – Jasmine Apr 15 '13 at 21:40
  • @Sotirios Your answer seems to be what I'm looking for I'll give it a go and try to implement it into the mobile app. Would Google GSON be able to deserialize JSON into a HashMap? – Luke Alderton Apr 15 '13 at 21:42
  • @Jasmine You're correct about the Java app and C# web service. – Luke Alderton Apr 15 '13 at 21:42
  • Yes, it absolutely can. – Sotirios Delimanolis Apr 15 '13 at 22:02
  • @Sotirios Your solution worked, I'd appreciate it if you could post it as an answer so I can mark as correct and sorry for the lack of detail in my original question. – Luke Alderton Apr 16 '13 at 20:54
  • @LukeAlderton I'm glad you got it working. I don't have all the details of an answer as I still don't quite understand the purpose of what you were doing. You're better off just putting up your own detailed solution. It'll probably be clearer to others since it's exact. – Sotirios Delimanolis Apr 16 '13 at 20:58
  • Okay sounds good to me. – Luke Alderton Apr 16 '13 at 21:06

1 Answers1

0
public class Page{

    int id;
    int parentId;
    String name;
    String title;
    String summary;
    String body;
    String updateDate;
    String createDate;

    public Page(int id, int parentId, String name, String title, String summary, String body, String update Date, String createDate)
    {
        this.id = id,
        this.parentId = parentId;
        this.name = name;
        this.title = title;
        this.summary = summary;
        this.body = body;
        this.updateDate = updateDate;
        this.createDate = createDate;
    }

}

...

Page page = new Page(1, 2, "name", "title", "summary", "body", "updateDate", "createDate");

...
Simon
  • 14,407
  • 8
  • 46
  • 61
  • This is what I currently have except I need to be able to add more/less parameters on the fly since the objects i'm going to be sending will be completely different every time. – Luke Alderton Apr 15 '13 at 21:33
  • If the arguments are completely different each time, then what is the point of an object since nothing else in your code can understand it. I think you need to explain *what* you are trying to do rather than ask how can I do it. – Simon Apr 15 '13 at 21:36
  • It's mostly going to be serialised/deserialized into and from JSON. – Luke Alderton Apr 15 '13 at 21:38