-1

This may be the most foolish question. But I just wanted to hear from the experience people. Why use JSON in Python? Is it to create code compatible or something like that? I once heard from my supervisor that someone will write the Python function and it will return JSON Object then the function can be used in Objective C? Is that even possible? Please Explain!! Thank you!

Sam Ad
  • 31
  • 1
  • 5

2 Answers2

1

As your example, when you need return a data from python to Object-C or any other env, you need a data-format to save the datas.

Json is a great data-format. From javascript but very like python dict.

Of cause you can define a new format by yourself. (Python.dict with eval, etc) But using json is easier:

  • you don't need to tell your workmates to learn your self-defined data format,
  • and there are a lot lib for json in any languages.

Json is string, not an "Object". So casting from one language(VM/ENV) to another is easy.

fanlix
  • 1,248
  • 1
  • 13
  • 22
  • please define "returning a data from python to Object-C". – Zaur Nasibov Aug 29 '12 at 09:42
  • @BasicWolf run a python vm in Object-C program, call python function do something, then the return value by Json format is returned to Object-C function, Object-C codes handle this json data at last. This is how, don't ask me why. – fanlix Aug 29 '12 at 09:49
  • @fanlix: Thank you so so much! I understood it really well! Thnx again mate! Appreciate it!! – Sam Ad Aug 29 '12 at 09:53
0

Suppose you do some computation in python that returns a list; and suppose this list needs to be later used by some C program.

Then, one way you can solve this issue is to write the list out into a file and make the C program read the list out of that file. In order to accomplish this, you need to write the list into the file in such a way that you can read it back correctly in the C program.

While there is nothing wrong with writing it as such, you might want to save some time in defining a file-storage protocol (or way of writing to and reading from file. It's actually called marshalling and de-marshalling or serialization and desierialization) and writing the necessary code to write to file and read from it. Thus, you might want to use popular marshalling and de-marshalling protocols for which most languages have libraries. JSON is one such popular marshalling protocol. Others include Pickle and protobuf.

Hope this helps

inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241