2014-09-22 5 views
0

json 파일이 있지만 iOS의 국제화 용 .strings 파일로 변환하고 싶습니다.JSON 대 iOS 문자열 파일

그래서,에서 가고 싶은 :

{"a": {"one": "b"}, 
"c" : {"title": "cool", "challenge": {"win": "score", "lose":"don't score"}} 
} 

같은 속으로 : 그것은 공통 기능 할 수있다처럼이, 내장 일 경우 내가 볼 것

"a.one" = "b" 
"c.title" = "cool" 
"c.challenge.win" = "score" 
"c.challenge.lose" = "don't score" 

생각 보인다.

감사합니다.

답변

1

내가 이런 일을 수행하는 JSON 모듈을 알고 아무것도하지만, JSON 데이터는 기본적으로 사전을 고려, 없다의 난 당신이 평평 할 생각

def to_listing(json_item, parent=None): 
    listing = [] 
    prefix = parent + '.' if parent else '' 

    for key in json_item.keys(): 
     if isinstance(json_item[key], basestring): 
      listing.append('"{}" = "{}"'.format(prefix+key,json_item[key])) 
     else: 
      listing.extend(to_listing(json_item[key], prefix+key)) 
    return listing 

In [117]: a = json.loads('{"a": {"one": "b"},"c" : {"title": "cool", "challenge": {"win": "score", "lose":"don\'t score"}}}') 

In [118]: to_listing(a) 
Out[118]: 
['"a.one" = "b"', 
'"c.challenge.win" = "score"', 
'"c.challenge.lose" = "don\'t score"', 
'"c.title" = "cool"'] 
1

손으로 그것을 아주 쉽게 사전? (https://stackoverflow.com/a/6027615/3622606에서 적응)

import collections 

def flatten(d, parent_key='', sep='.'): 
    items = [] 
    for k, v in d.items(): 
    new_key = parent_key + sep + k if parent_key else k 
    if isinstance(v, collections.MutableMapping): 
     items.extend(flatten(v, new_key).items()) 
    else: 
     items.append((new_key, v)) 
    return dict(items) 

dictionary = {"a": {"one": "b"}, "c" : {"title": "cool", "challenge": {"win": "score",  "lose":"don't score"}}} 

flattened_dict = flatten(dictionary) 

# Returns the below dict... 
# {"a.one": "b", "c.title": "cool", "c.challenge.win": "score", "c.challenge.lose": "don't score"} 
# And now print them out... 

for value in flattened_dict: 
    print '"%s" = "%s"' % (value, flattened_dict[value]) 

# "c.challenge.win" = "score" 
# "c.title" = "cool" 
# "a.one" = "b" 
# "c.challenge.lose" = "don't score" 
+0

정확히 내가 원하는 것을, 감사합니다! 처음 제출 한 다른 제출자에게 전달하십시오! – Jonovono