-3

i am trying to parse a json array,i am facing problem.

My array is like this:

configure: {"id":4,"userId":107,"deviceMacAddress":"00:06:66:30:02:3C","medication":

[{"id":11,"version":18,"name":"name1","unit":"mg","forMed":"for1","schedule":[1]},{"id":45,"version":1,"name":"sdga",,"unit":"mg","forMed":"54234","schedule":[0,1,2,3,4,5,6]}],

i am able to access medication array and print total array,but not able to access objects inside array. can you pls suggest any solution or any example to do this using C language?

David DeMar
  • 2,390
  • 2
  • 32
  • 45
lenin T.mohan
  • 135
  • 1
  • 1
  • 4
  • 1
    http://stackoverflow.com/helpcenter/dont-ask - Stack Overflow is for asking objective questions. You need to read about either grammars or finite state machines. Or you can ask a specific question about code you've tried and tell us what it's not doing and how to fix a specific problem. Also, proper english is highly encouraged. "Text speak" is frowned upon. – xaxxon Jun 07 '13 at 04:21
  • Also, the most trivial google search returns obvious results: https://www.google.com/search?q=json+c+parser&oq=json+c+parser&aqs=chrome.0.57j60j0l2j62.1855j0&sourceid=chrome&ie=UTF-8 – xaxxon Jun 07 '13 at 04:22
  • Given JSON is not correct or Not Complete. – Navnath Godse Jun 07 '13 at 04:22
  • @Navnath what exactly do you mean? Just that it doesn't require a full-on CFG to describe? – xaxxon Jun 07 '13 at 04:27
  • @xaxxon I mean to say, the given JSON is syntactically incorrect. – Navnath Godse Jun 07 '13 at 04:31
  • This is a duplicate of http://stackoverflow.com/questions/16975918/json-array-parsing-in-c – Aiias Jun 07 '13 at 05:32

1 Answers1

1

I'll throw you a bone. When you find a sigil representing the beginning of an element in the text, call a function that knows how to handle that type of element - hash, array, string, number, etc (http://www.json.org/). Now how you want to handle things like a hash is up to you, and C makes it a bit difficult to store the different types of values easily, but basically in each section, you return the object representing that and it gets turned into a overall data structure representing the json you just parsed.

VERY pseudocode:

parseJson(char* json) {
  if json[0]=='['
    parseJsonArray(json+1)
  if json[0]=='{]
    parseJsonArray(json+1)
  if json[0]=='"'
    parseJsonString(json+1)
  ...etc...
}

parseJsonArray(char* json) {
  SomeSortOfList array = MakeListThing();
  while *json != ']' {
    if *json=='['
      push(array, parseJsonArray(json+1))
    if *json=='{'
      push(array, parseJsonHash(json+1))
    if *json=='"'
      push(array, parseJsonString(json+1))
  }
  return array
}

..and other functions..

xaxxon
  • 19,189
  • 5
  • 50
  • 80