-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?

6
  • 1
    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. Commented Jun 7, 2013 at 4:21
  • Also, the most trivial google search returns obvious results: google.com/… Commented Jun 7, 2013 at 4:22
  • Given JSON is not correct or Not Complete. Commented Jun 7, 2013 at 4:22
  • @Navnath what exactly do you mean? Just that it doesn't require a full-on CFG to describe? Commented Jun 7, 2013 at 4:27
  • @xaxxon I mean to say, the given JSON is syntactically incorrect. Commented Jun 7, 2013 at 4:31

1 Answer 1

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..

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.